2012年亚洲长春区域赛:E Conquer a New Region

E - Conquer a New Region

E - Conquer a New Region
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

 

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 capacityC(ij) of a road indicating it is allowed to transport at most C(ij) 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(ij) 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$ \le$N$ \le$200, 000)

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

Output 

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

Sample Input 

4
1 2 2
2 4 1
2 3 1
4
1 2 1
2 4 1
2 3 1

Sample Output 

4
3

 解题思路:因为一条路上的运输力是限制于路上最小的流量, 那麽边按流量大到小排序后,再进行处理。

解题代码:

View Code
 1 // File Name: E - Conquer a New Region
 2 // Author: sheng
 3 // Created Time: 2013年04月18日 星期四 10时31分37秒
 4 
 5 #include <iostream>
 6 #include <stdio.h>
 7 #include <string.h>
 8 #include <algorithm>
 9 using namespace std;
10 const int Maxn = 2e5+5;
11 typedef long long LL;
12 
13 struct node
14 {
15     int x, y;
16     int w;
17     bool operator < (const node t1) const
18     {
19         return w > t1.w;
20     }
21 }town[Maxn];
22 
23 LL sum[Maxn];
24 int cnt[Maxn];
25 int fa[Maxn];
26 
27 int find(int x)
28 {
29     if (x != fa[x])
30         return fa[x] = find(fa[x]);
31     return x;
32 }
33 
34 int main()
35 {
36     int i, n, a, b, c;
37     while (scanf ("%d", &n) == 1)
38     {
39         for (i = 1; i < n; i ++)
40             scanf ("%d%d%d", &town[i].x, &town[i].y, &town[i].w );
41         for (i = 1; i <= n; i ++)
42         {
43             sum[i] = 0;
44             cnt[i] = 1;
45             fa[i] = i;
46         }
47         sort (town + 1, town + n);
48         LL ans = 0;
49         for (i = 1; i < n; i ++)
50         {
51             a = find (town[i].x);
52             b = find (town[i].y);
53             LL atob = (LL) cnt[a] * town[i].w + sum[b];
54             LL btoa = (LL) cnt[b] * town[i].w + sum[a];
55             if (atob > btoa )
56             {
57                 fa[a] = b;
58                 cnt[b] += cnt[a];
59                 sum[b] = atob;
60             }
61             else
62             {
63                 fa[b] = a;
64                 cnt[a] += cnt[b];
65                 sum[a] = btoa;
66             }
67             ans = max (ans, max(atob, btoa));
68         }
69         printf ("%lld\n", ans);
70     }
71 }
 
原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3028240.html