码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 3684 Destroy

时间:2015-10-03 10:41:00      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

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

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 }
View Code

 

ZOJ 3684 Destroy

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4853101.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!