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. 1-3Controller之Response

    控制器中的方法: public function response1(){ /*响应的常见类型: * 1.字符串 * 2.视图 * 3.json * 4.重定向 * */ //响应JSON /*$da ...

  2. QuickStart系列:docker部署之redis

    在centos7的docker中部署 redis,这里只介绍 单节点的部署. docker run -p 6379:6379 -v $PWD/data:/data -d redis:latest re ...

  3. python2和python3的区别总结

    python2x和python3x区别: python2x:源码重复,不规范. python3x:  源码规范,优美,清晰,简单. 编译型:将代码一次性全部转化成字节码. 代表语言:C,C++ 优点: ...

  4. DevExpress v18.1新版亮点——Office File API篇

    用户界面套包DevExpress v18.1日前正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress Office File API v18.1 的新功能,快来下载试 ...

  5. python中的import,reload,以及__import__

    python中的import,reload,以及__import__ 分类: UNIX/LINUX C/C++LINUX/UNIX shellpython2013-04-24 20:294536人阅读 ...

  6. Iscloc用法笔记

    一. Iscloc的安装(使用Docker CE) 1.install Docker CE: https://www.digitalocean.com/community/tutorials/how- ...

  7. elk之kibana

    环境: centos7 jdk8 参考: https://www.elastic.co/guide/en/elasticsearch/reference/current/rpm.htmlhttp:// ...

  8. ubuntu apache ssl配置

    参考连接: http://blog.csdn.net/sky_qing/article/details/44303221 http://blog.sina.com.cn/s/blog_6ad62438 ...

  9. shell脚本实例-脚本批量创建用户

    #!/usr/bin/bash read -p "Please input number: " num if [[ ! "$num" =~ ^[0-9]+$ | ...

  10. php优秀框架codeigniter学习系列——index.php

    程序流程图 先来看看CI框架运行的程序流程图. 从图中我们 看到,index.php作为唯一的入口文件,会初始化CI框架运行所需的基本资源. 路由器(Routing)会根据http请求,确定如何处理: ...