HDOJ 4424 Conquer a New Region
2015-09-14 11:43
477 查看
并检查集合
侧降序,每增加一个侧面应该推断,其中基本建设方.....
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1169 Accepted Submission(s): 373
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
Sample Output
Source
2012 Asia ChangChun Regional Contest
侧降序,每增加一个侧面应该推断,其中基本建设方.....
Conquer a New Region
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1169 Accepted Submission(s): 373
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
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
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=200200; typedef long long int LL; struct Edge { LL u,v,w; }edge[maxn]; bool cmp(Edge x,Edge y) { return x.w>y.w; } LL n; LL fa[maxn]; LL value[maxn]; LL sz[maxn]; LL find(LL x) { if(x==fa[x]) return x; return fa[x]=find(fa[x]); } int main() { while(scanf("%I64d",&n)!=EOF) { for(LL i=0;i<n-1;i++) { LL a,b,c; scanf("%I64d%I64d%I64d",&a,&b,&c); edge[i].u=a; edge[i].v=b; edge[i].w=c; fa[i]=i;sz[i]=1;value[i]=0; } fa[n-1]=n-1; sz[n-1]=1; value[n-1]=0; fa =n;sz =1;value =0; sort(edge,edge+n-1,cmp); for(LL i=0;i<n-1;i++) { LL u=edge[i].u,v=edge[i].v; LL w=edge[i].w; LL U=find(u),V=find(v); if(U==V) continue; LL VVV=max(value[U]+w*sz[V],value[V]+w*sz[u]); fa[U]=V; value[V]=VVV; sz[V]=sz[V]+sz[U]; } cout<<value[find(1)]<<endl; } return 0; }
相关文章推荐
- 看unix高级编程(第三版)时遇到apue.h找不到的问题
- erl_stack_queue-队列求迷宫最短路径
- HiWork发布1.6.0版本——调整UI布局,新增消息搜索等多项功能
- erl_stack_queue-利用栈实现迷宫的求解
- How-to: write own Kafka Partitioner based on requirement
- Unix环境高级编程开篇-"apue.h"配置
- qt使用uic命令将后缀名为.ui的文件输出成.h文件方法
- UITextField使用总结
- UITableView+FDTemplateLayoutCell源码学习笔记
- js1:根据标签的Id获取value值
- iOS UI基础-6.0 UIActionSheet的使用
- Permutation sequence
- IOS UI学习 UISearchBar
- iOS小技巧:用runtime 解决UIButton 重复点击问题
- document.getElementById和document.querySelector的区别
- int、NSInteger、NSUInteger、NSNumber的区别和联系
- 解决request.getParament()空指针问题和字符的乱码问题,get和post都没有问题
- iOS开发UI篇—从代码的逐步优化看MVC
- iOS开发UI篇—字典转模型
- UISegmentedControl的详细使用