Conquer a New Region

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1271    Accepted Submission(s): 415

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
题意:给出一个树形图和边权值,边权值是容量,问把某个点作为首都,向其他n-1个点运输货物,货物容量不超过路径上的边权值;问可以输出的最大货物是多少?
分析:并查集,按照边权从大到小排序,在加入该边的同时,比较把两个集合合并到那个集合更优:
#include"string.h"
#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"queue"
#include"stack"
#include"stdlib.h"
#include"map"
#include"string"
#include"math.h"
#define inf 10000000
#define INF 0x3f3f3f3f
const double PI=acos(-1.0);
const double r2=sqrt(2.0);
const int M=200009;
const int N=1010*502*2;
const double g=9.8;
#define eps 1e-10
using namespace std;
int f[M],h[M];
__int64 sum[M],ans;
int finde(int x)
{
if(x!=f[x])
f[x]=finde(f[x]);
return f[x];
}
void make(int a,int b,__int64 w)
{
int x=finde(a);
int y=finde(b);
__int64 f1,f2;
f1=sum[x]+h[y]*w;
f2=sum[y]+h[x]*w;
if(f1>f2)
{
f[y]=x;
sum[x]+=h[y]*w;
h[x]+=h[y];
}
else
{
f[x]=y;
sum[y]+=h[x]*w;
h[y]+=h[x];
}
}
struct node
{
int u,v;
__int64 w;
}e[M];
int cmp(node a,node b)
{
return a.w>b.w;
}
int main()
{
int n,i;
while(scanf("%d",&n)!=-1)
{
for(i=1;i<=n;i++)
{
f[i]=i;
sum[i]=0;
h[i]=1;
}
ans=0;
for(i=1;i<n;i++)
scanf("%d%d%I64d",&e[i].u,&e[i].v,&e[i].w);
sort(e+1,e+n,cmp);
for(i=1;i<n;i++)
make(e[i].u,e[i].v,e[i].w);
printf("%I64d\n",sum[finde(1)]);
}
return 0;
}

并查集hdu4424的更多相关文章

  1. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  2. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  3. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  4. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  5. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  6. [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

  7. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  8. Codeforces 731C Socks 并查集

    题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...

  9. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

随机推荐

  1. LoadRunner性能分析指标解释

    Transactions(用户事务分析) 用户事务分析是站在用户角度进行的基础性能分析. 1.Transation Sunmmary(事务综述) 对事务进行综合分析是性能分析的第一步,通过分析测试时间 ...

  2. Windows 10恢复Shift+右键打开命令提示符窗口

    导入以下注册表即可. Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\Background\shell\cmd1] ...

  3. QT 交叉编译工具选择

    使用QT交叉编译,生成的都是x86的可执行文件.Zoro告诉我交叉工具配置错了. 参考链接: http://www.cnblogs.com/zengjfgit/p/4744507.html linux ...

  4. 在 C++ 程序中只使用 const 常量而不使用宏常量

    在 C++ 程序中只使用 const 常量而不使用宏常量,即 const 常量完 全取代宏常量. #include <iostream> /* run this program using ...

  5. 用 #include <filename.h> 格式来引用标准库的头文件

    用 #include <filename.h> 格式来引用标准库的头文件(编译器将从 标准库目录开始搜索). #include <iostream> /* run this p ...

  6. 嵌入式驱动开发之usb 无线网卡驱动---RT2870STA dm368

    RT2870STA 368的无线网卡驱动! http://www.linuxidc.com/Linux/2014-02/96979.htm

  7. python2.0_day19_前端分页功能的实现

    我们前面完成的客户纪录展示,只有4条,如果有上百条就不能在1页中全部展示了,那样是不人性化的.另外一次性取出来,数据量也比较大.假如现在有95条数据,我们想实现一个每页展示20条,那就分为5页.假如我 ...

  8. 06python 之基本数据类型

    数字 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483646 在64位机器上,整数的位数为64位,取值范围为-2** ...

  9. 转的:burp suite小例子

    Web安全测试时经常会遇到一些蹩脚的注射点,而因各种原因利用注射又无法获取网站管理账号或拥有网站管理权限却迟迟不能upload一个shell的时候,可能会权衡一下web权限与数据库信息,哪个是我们所需 ...

  10. 查看内存使用情况:free

    free命令用于显示内存的使用情况,常见用法如下: [root@localhost ~]$ free # 以KB为单位显示内存使用情况 [root@localhost ~]$ free -m # 以M ...