HDU 4424 并查集+贪心思想
Conquer a New Region
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1939 Accepted Submission(s): 673
Total Submission(s): 1939 Accepted Submission(s): 673
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.
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)
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
4 1 2 2 2 4 1 2 3 1 4 1 2 1 2 4 1 2 3 1
Sample Output
4 3
Source
2012 Asia ChangChun Regional Contest
题意:n个点,n-1条边,肯定就是一个树了,用并查集的话,一个树就是一个集合,这个题加上贪心思想觉得还是比较难的。
思路:
题意:n个点,n-1条边,肯定就是一个树了,用并查集的话,一个树就是一个集合,这个题加上贪心思想觉得还是比较难的。
思路:
由于要所有点到这个点的权值和最大,把边按从大到小排序并插入。每条边连接两个集合,且每次并入的边权值都是当前已并入边中最小的。那么,只要每次并入时判断是把a并入b得到的权值和大,还是b并入a得到的权值和大就可以了。并查集维护集合的元素个数和总的权值。看了网上的觉得没有一个讲的详细的,决定在这里用数据模拟一下。
数据:
5
3 2 1
2 1 2
4 2 3
1 5 4
经过排序后:
5
1-5 4
4-2 3
2-1 2
3-2 1
![](//img-blog.csdn.net/20160812104858933?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](//img-blog.csdn.net/20160812104904449?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](//img-blog.csdn.net/20160812104908510?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
模拟一边之后就知道该算法的基本思想,详情看代码注释:
数据:
5
3 2 1
2 1 2
4 2 3
1 5 4
经过排序后:
5
1-5 4
4-2 3
2-1 2
3-2 1
模拟一边之后就知道该算法的基本思想,详情看代码注释:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn =200000+10; typedef long long LL; //自定义类型 int f[MAXN]; LL sum[MAXN]; int cnt[MAXN]; struct Edge{ int u,v,w; }ee[MAXN]; int n; void Initiate() { memset(sum,0,(n+2)*sizeof(sum[0])); for(int i=0;i<=n;i++){ f[i]=i; cnt[i]=1;//开始的每个点相当于一个树,每棵树只有一个节点 } } int Find(int x) { if(x==f[x]) return x; f[x]=Find(f[x]); return f[x]; } void Union(int r1,int r2,LL w) { f[r1]=r2; cnt[r2]+=cnt[r1];//新连的边到各个点的条数,因为新加边肯定最小,所以在tmp中新加的w直接乘与路程数目,再加上自己的sum,可以得到新的总权值 sum[r2]+=w; //tmp中r2和r1相反,如果是以新加点为中心,则加的是中心点以前的sum和,就比如还是以老根为中心,要加的上个sum就是这个,而cnt[新点]*w则是新加的 } int cmp( Edge p, Edge q) { return p.w>q.w; } int main() { //freopen("in.txt","r",stdin); while(~scanf("%d",&n)) { Initiate(); for(int i=0;i<n-1;i++){ scanf("%d%d%d",&ee[i].u,&ee[i].v,&ee[i].w); } sort(ee,ee+n-1,cmp); for(int i=0;i<n-1;i++){ int r1=Find(ee[i].u); int r2=Find(ee[i].v); LL w=(LL)ee[i].w; LL tmp1=cnt[r2]*w+sum[r1];//方案的r1为根 LL tmp2=cnt[r1]*w+sum[r2]; if(tmp1>tmp2){ Union(r2,r1,tmp1-sum[r1]); }else Union(r1,r2,tmp2-sum[r2]);//tmp2-sum[r2]为cnt[r1]*w,先说w为这个树里面最小的权值,那么w*cnt(节点数)肯定是这个树最大的权值和, //加入的下一个点则是选择以这个点为中心还是以这个树的某一个点中为中心(由新加的的点的边来决定),比较两种方案的总权值和。 //此时若是以旧树的点为中的话,直接加cnt[新点]*w+旧树权值和,然后把新点合并旧树组成新树sum[这个树的根]继续更新。若是以新点 //为中心,则是cnt[旧树根](cnt[旧树根]为旧树的节点)*w,(因为此时新加的w是最小的),加上sum[新点]。比较两种方案大小后,合并 //确定是新点为新树的根(则此时是w*旧树节点树),sum[新点]更新,还是旧树根吸收新点。 } printf("%I64d\n",sum[Find(1)]);//随便输入这个树的一个节点 } return 0; }