ZOJ 3659 Conquer a New Region

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   java   ar   for   

Conquer a New RegionTime Limit: 5000msMemory Limit: 32768KBThis problem will be judged on ZJU. Original ID: 3659
64-bit integer IO format: %lld      Java class name: Main 

The wheel of the history rolling forward, our king conquered a new region in a distant continent.

There are N towns (numbered from 1 to N) in this region connected by several roads. It‘s confirmed that there is exact one route between any two towns. Traffic is important while controlled colonies are far away from the local country. We define the capacity C(i, j) of a road indicating it is allowed to transport at most C(i, j) goods between town i and town j if there is a road between them. And for a route between i and j, we define a value S(i, j) indicating the maximum traffic capacity between i and j which is equal to the minimum capacity of the roads on the route.

Our king wants to select a center town to restore his war-resources in which the total traffic capacities from the center to the other N - 1 towns is maximized. Now, you, the best programmer in the kingdom, should help our king to select this center.

Input

There are multiple test cases.

The first line of each case contains an integer N. (1 ≤ N ≤ 200,000)

The next N - 1 lines each contains three integers a, b, c indicating there is a road between town a and town b whose capacity is c. (1 ≤ a, b ≤ N, 1 ≤ c ≤ 100,000)

Output

For each test case, output an integer indicating the total traffic capacity of the chosen center town.

Sample Input
41 2 22 4 12 3 141 2 12 4 12 3 1
Sample Output
43
 SourceThe 2012 ACM-ICPC Asia Changchun Regional Contest 解題:並查集的進階應用程式。 
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 200100;18 struct arc{19     int u,v,w;20     arc(int x = 0,int y = 0,int z = 0){21         u = x;22         v = y;23         w = z;24     }25 };26 int uf[maxn],rank_[maxn],tot;27 LL d[maxn];28 arc e[maxn];29 bool cmp(const arc &x,const arc &y){30     return x.w > y.w;31 }32 int Find(int x){33     if(x == uf[x]) return uf[x];34     return uf[x] = Find(uf[x]);35 }36 void Union(int x,int y,LL z){37     uf[x] = y;38     rank_[y] += rank_[x];39     d[y] = z;40 }41 int main() {42     int n;43     while(~scanf("%d",&n)){44         for(int i = 1; i < n; i++)45             scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w);46         for(int i = 0; i < maxn; i++){47             uf[i] = i;48             rank_[i] = 1;49             d[i] = 0;50         }51         sort(e+1,e+n,cmp);52         for(int i = 1; i < n; i++){53             int x = Find(e[i].u);54             int y = Find(e[i].v);55             LL sx = d[x] + (LL)e[i].w*rank_[y];56             LL sy = d[y] + (LL)e[i].w*rank_[x];57             if(sx > sy) Union(y,x,sx);58             else Union(x,y,sy);59         }60         printf("%I64d\n",d[Find(1)]);61     }62     return 0;63 }
View Code

 

ZOJ 3659 Conquer a New Region

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.