Destroy
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:
- 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)
- 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)
- The length of each road.
- 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
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 ",dp[root]); 76 } 77 return 0; 78 }