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.

题意:有若干个城市,城市之间有边,边有容量,现在要求一个城市,使它到所有城市的总运量最大,到某一城市的运量等于相互路径上的最小容量。

由于运量是由最小容量限制的,所以我们可以先构建一颗最大生成树,边构建树边计算权值。将边从大到小排序之后,每次将最大权的边进行合并,合并的两个块内部的边都是在之前合并的,因此边权一定比当前合并的边权大,所以这两块之间运输的相互运量均等于当前边的边权,这样就可以边合并边计算合并后的最大运量,即等于某一边的最大运量加上边权乘另一边的点数个数,两边取最大值即为合并后的最大权值。

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int mod=1e9+;
const int maxn=2e6+; struct seg{
int a,b;
ll v;
bool operator <(const seg x)const{
return v>x.v;
}
}s[maxn]; int fa[maxn];
ll sz[maxn];
ll ma[maxn],ans;
int n; int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);} void unio(int i){
int x=s[i].a,y=s[i].b;
ll v=s[i].v;
int X=find(x),Y=find(y);
ll nx=ma[X]+v*sz[Y];
ll ny=ma[Y]+v*sz[X];
if(nx>=ny){
fa[Y]=X;
sz[X]+=sz[Y];
ma[X]=nx;
if(nx>ans)ans=nx;
}
else{
fa[X]=Y;
sz[Y]+=sz[X];
ma[Y]=ny;
if(ny>ans)ans=ny;
}
} void init(){
ans=;
for(int i=;i<=n+;++i)fa[i]=i;
for(int i=;i<=n+;++i)sz[i]=;
for(int i=;i<=n+;++i)ma[i]=;
} int main(){
while(scanf("%d",&n)!=EOF){
if(n==){printf("0\n");continue;}
init();
for(int i=;i<n;++i)scanf("%d%d%lld",&s[i].a,&s[i].b,&s[i].v);
sort(s+,s+n-+);
for(int i=;i<n;++i)unio(i);
printf("%lld\n",ans);
}
return ;
}

hdu4424 Conquer a New Region 并查集/类似最小生成树的更多相关文章

  1. hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)

    Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  2. ZOJ3659 Conquer a New Region 并查集

    Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history roll ...

  3. ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)

    这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...

  4. zoj 3659 Conquer a New Region(并查集)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...

  5. hdu 4424 Conquer a New Region (并查集)

    ///题意:给出一棵树.树的边上都有边权值,求从一点出发的权值和最大,权值为从一点出去路径上边权的最小值 # include <stdio.h> # include <algorit ...

  6. POJ-2421Constructing Roads,又是最小生成树,和第八届河南省赛的引水工程惊人的相似,并查集与最小生成树的灵活与能用,水过~~~

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K               Description There are N v ...

  7. hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)

    还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  8. hdu 1233 还是畅通工程 并查集or最小生成树

    某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路 ...

  9. POJ 3723 Conscription (Kruskal并查集求最小生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14661   Accepted: 5102 Des ...

随机推荐

  1. JavaScript中如何对一个对象进行深度clone

    <!doctype html><html><head><meta charset="utf-8"><title>深克隆& ...

  2. Linux运维工程师真实的工作状态到底是怎么样的?

    现在的运维工程师在大家眼中是个什么样子呢? 是不是还是把服务器搬来搬去,每天不是在拿着Linux光盘开始装系统,就是在等待系统安装完成.你如果还是这么想,那就大错特错了.现在又有做一个新的物种诞生,那 ...

  3. day13-类的重写和类的私有方法

    类的重写 在python中 有时需要进行重写,重写是继承机制中的一个重要部分, 可以重写一般方法也可以重写构造方法,构造方法是用来初始化新创建对象的状态. class A : def hello(se ...

  4. mysql 从库执行insert失败导致同步停止

    服务配置:一主一从,版本都是 5.5 .主库配置了 binlog-do-db binlog-ignore-db 问题复述:运营人员发现,昨天的数据统计不对.数据分析服务查询的是从库的数据. 到tomc ...

  5. 非递归实现二叉树的三种遍历操作,C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  6. jQuery操错题积累

    1: 解析: onBlur:焦点移除事件. onfocus:定义和用法 onfocus 事件在对象获得焦点时发生 onchange:定义和用法 onchange 事件会在域的内容改变时发生 nclic ...

  7. Bootstrap--思维导图

    Bootstrap--思维导图

  8. 小程序之setData特殊情况 三种情况的wx:if

    比如data{ “a”:{}, "b":{} } 你想完成这样的结构 //创建一个对象 var readyData={} //对象[key] =另一个对象 readyData[ke ...

  9. DevExpress WinForms v18.2新版亮点(六)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v1 ...

  10. POJ - 2635 E - The Embarrassed Cryptographer

    The young and very promising cryptographer Odd Even has implemented the security module of a large s ...