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.
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
41 2 22 4 12 3 141 2 12 4 12 3 1
Sample Output
43
Source
2012 Asia ChangChun Regional Contest
思路:先按权值排序由大到小,然后每加入一条边按照价值判断谁并入谁。
# 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
- HDU4424:Conquer a New Region(并查集 + 贪心)
- HDU4424--Conquer a New Region
- hdu4424 Conquer a New Region
- hdu 4424 Conquer a New Region (贪心+并查集)
- ZOJ3659 - Conquer a New Region - 并查集+贪心算法
- zoj 3659 Conquer a New Region 并查集+贪心
- 【并查集】 HDU 4424 Conquer a New Region 贪心
- HDU 4424 Conquer a New Region【贪心+并查集】
- UVA 1664 Conquer a New Region (并查集+贪心)
- UVA 1664 Conquer a New Region (并查集+贪心)
- hdu 4424 Conquer a New Region(并查集+贪心)
- hdu 4424 Conquer a New Region(并查集)
- ZOJ Conquer a New Region(并查集)
- UVA 1664 - Conquer a New Region(并查集)
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
- 并查集+贪心 Conquer a New Region HDU 4118 && ZOJ 3659
- ZOJ 3659 Conquer a New Region(并查集)
- Hdu 4424 Conquer a New Region 并查集
- 安卓-启动前台服务
- Parallel 线程
- 第27天(就业班) 泛型、泛型的反射、注解
- CGI 与 fast_cgi
- SVM支持向量机原理(三)线性不可分支持向量机与核函数
- HDU4424:Conquer a New Region(并查集 + 贪心)
- Android Studio Plugins
- android倒计时控件
- JIT与JVM的三种执行模式:解释模式、编译模式、混合模式
- vagrant的配置文件vagrantfile详解
- CentOs6.5安装和配置JDK7、TOMCAT7
- 港版-已过保 MacBookPro因显示屏涂层脱落去"天才吧"免费换屏过程
- gitbook 放弃search-jieba插件
- MySQL的简单查询语句