HDU4424:Conquer a New Region(并查集 + 贪心)

来源:互联网 发布:广州打车软件 编辑:程序博客网 时间:2024/05/20 01:13

Conquer a New Region

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2050    Accepted Submission(s): 716


Problem 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 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
 

Source
2012 Asia ChangChun Regional Contest

题意:给你N个点和N-1条边的连通图,也就是说任意两点间的路径是唯一的。每条边有个权值,从一点到另一点的最大流量是路径中所有边中权值的最小值。要你找一个点,使得这点到剩下所有点的最大流量之和最大,求这个最大流量和。

思路:先按权值排序由大到小,然后每加入一条边按照价值判断谁并入谁。

# include <iostream># include <cstdio># include <algorithm># define MAXN 200000using namespace std;struct node{    int a, b, c;}e[MAXN+3];int pre[MAXN+3], sum[MAXN+3];long long num[MAXN+3];void init(int n){    for(int i=0; i<=n; ++i)    {        pre[i] = i;        sum[i] = 1;        num[i] = 0;    }}int find(int x){    if(x != pre[x])        pre[x] = find(pre[x]);    return pre[x];}bool cmp(node a, node b){    return a.c > b.c;}int main(){    int n;    while(~scanf("%d",&n))    {        init(n);        long long ans = -1;        for(int i=1; i<n; ++i)           scanf("%d%d%d",&e[i].a, &e[i].b, &e[i].c);        sort(e+1, e+n, cmp);        for(int i=1; i<n; ++i)        {            int px = find(e[i].a);            int py = find(e[i].b);            long long pxx = num[px] + (long long)e[i].c * sum[py];            long long pyy = num[py] + (long long)e[i].c * sum[px];            if(pxx > pyy)            {                pre[py] = px;                num[px] = pxx;                sum[px] += sum[py];            }            else            {                pre[px] = py;                num[py] = pyy;                sum[py] += sum[px];            }            ans = max(ans, max(pxx,pyy));        }        printf("%I64d\n",ans);    }    return 0;}



0 0
原创粉丝点击