ZOJ 3684 Destroy

Destroy

Time Limit: 2000ms

Memory Limit: 65536KB

This problem will be judged on ZJU. Original ID: 3684
64-bit integer IO format: %lld      Java class name: Main

DJT country and CG country are always on wars.
This time DJT‘s King built a new information system over the whole country. He wants to know the message from the frontier immediately. There are numerous cities in DJT. In every city, there is one server of this system, and information is sending to the center continuously along a special data road. However, the data road is so expensive that there is one and only one road from one city to another city. Besides, the place of the center is a secret.

CG, of course, won‘t let DJT be happy for too long. CG is now planning to destroy DJT‘s new system. Due to some great undercover agents, CG has controlled some information about DJT‘s new system. The information CG has got:

  1. The center of DJT‘s new system would settle down in a city that for all other cities, the maximum distance should be the least.(you can make sure that only one city has the possibility to be the center)
  2. If no frontier city could send message back to the center, the system can be regard as destroyed. (a frontier city is a city that has only one road connecting to it)
  3. The length of each road.
  4. The power we need to destroy each road. (if we have a weapon of power max, we can destroy all the roads which it need the power less or equal to max)

Now, CG gives you a task: calculate the minimum power to destroy the system.

Input

There are multiple cases. For each case, one integer n (0 <= n <= 10000) indicating the number of cities in DJY country, cities are numbered from 1 to n, the next n-1 lines, one line contains four numbers describing one road, the two cities connected by the road, the length, and the power needed to destroy. The lengths are less than or equal to 10000. The powers are less than or equal to 100000000. All integers are nonnegative.

Output

For each case, output one number indicating the least power we need.

Sample Input

9
1 4 1 3
2 3 1 7
2 5 1 2
4 5 1 5
5 6 1 4
5 8 1 4
6 9 1 4
7 8 1 6

Sample Output

4

Source

ZOJ Monthly, January 2013

Author

HE, Ningxu

解题:树的直径+树形dp(或者二分)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 const int INF = 0x3f3f3f3f;
 5 const int maxn = 10010;
 6 struct arc{
 7     int to,len,power,next;
 8     arc(int x = 0,int y = 0,int z = 0,int nxt =-1){
 9         to = x;
10         len = y;
11         power = z;
12         next = nxt;
13     }
14 }e[maxn<<1];
15 int head[maxn],d[maxn],p[maxn],tot,n,S,T;
16 int dp[maxn];
17 void add(int u,int v,int len,int power){
18     e[tot] = arc(v,len,power,head[u]);
19     head[u] = tot++;
20 }
21 queue<int>q;
22 int bfs(int u){
23     memset(d,-1,sizeof d);
24     memset(p,-1,sizeof p);
25     d[u] = 0;
26     while(!q.empty()) q.pop();
27     q.push(u);
28     int ret = u;
29     while(!q.empty()){
30         u = q.front();
31         q.pop();
32         if(d[u] > d[ret]) ret = u;
33         for(int i = head[u]; ~i; i = e[i].next){
34             if(d[e[i].to] == -1){
35                 d[e[i].to] = d[u] + e[i].len;
36                 p[e[i].to] = u;
37                 q.push(e[i].to);
38             }
39         }
40     }
41     return ret;
42 }
43 void dfs(int u,int fa){
44     int tmp = 0;
45     dp[u] = 0x3f3f3f3f;
46     bool flag = false;
47     for(int i = head[u]; ~i; i = e[i].next){
48         if(e[i].to == fa) continue;
49         dfs(e[i].to,u);
50         tmp = max(tmp,min(e[i].power,dp[e[i].to]));
51         flag = true;
52     }
53     if(flag) dp[u] = min(dp[u],tmp);
54 }
55 int main(){
56     int u,v,L,P;
57     while(~scanf("%d",&n)){
58         memset(head,-1,sizeof head);
59         int root = tot = 0,mx = INF;
60         for(int i = 1; i < n; ++i){
61             scanf("%d%d%d%d",&u,&v,&L,&P);
62             add(u,v,L,P);
63             add(v,u,L,P);
64         }
65         u = T = bfs(S = bfs(1));
66         while(~u){
67             int tmp = max(d[T] - d[u],d[u]);
68             if(tmp < mx){
69                 mx = tmp;
70                 root = u;
71             }
72             u = p[u];
73         }
74         dfs(root,-1);
75         printf("%d\n",dp[root]);
76     }
77     return 0;
78 }

时间: 2025-02-01 08:56:14

ZOJ 3684 Destroy的相关文章

ZOJ 3684 Destroy 树的中心

中心节点就是树的中心,2遍dfs求到树的直径,而中心一定在直径上,顺着直径找到中心就够了. 然后可以一遍树形DP找到最小值或者二分+判断是否访问到叶子节点. #include <iostream> #include<vector> #include<cstdio> #include<algorithm> using namespace std; struct node { int next; int power; int length; }t; vector

2014 8.8 第9场个人 排位

UVA 12657 Boxes in a Line 模拟 FZU 1876 JinYueTuan 不说话 view code#include <iostream> using namespace std; int main() { long long n,m,p,ans=1; while(cin>>n>>m>>p){ ans=1; for(int i=n;i<n+m;i++){ ans=ans*i%p; if(ans==0)break; } cout&

2014 Super Training #9 E Destroy --树的直径+树形DP

原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其他点的最远距离最小).现在你要破坏所有叶子节点到根节点的连通,每条边破坏都需要一定能量.你有一个能量为power的武器,能破坏能量小于等于power的任何路.求最少需要的power. 解法参考博客:http://blog.csdn.net/gzh1992n/article/details/86511

ZOJ 3792 Romantic Value 最小割+求割边的数量

点击打开链接 Romantic Value Time Limit: 2 Seconds      Memory Limit: 65536 KB Farmer John is a diligent man. He spent a lot of time building roads between his farms. From his point of view, every road is romantic because the scenery along it is very harmon

ZOJ 1002 Fire Net

columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting thro

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy