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. c语言亲缘线程通过管道通信一些疑问

    亲缘线程在使用管道时,发现第一次使用管道进行进行通信完全正常(./a.out 1),但当重新运行并使用新管道文件时候出现数据无法读取的问题(./a.out 2)(./a.out 3),甚至出现子线程部 ...

  2. python-面向对象增强版

    class Person: def __init__(self, name, id, gender, birth): self.name = name # 实例变量 对象里的变量 self.id = ...

  3. 遍历存储所有物体添加到列表中(使用GameObject.activeSelf进行判断)

    //存储菜单列表 List<GameObject> subMenu = new List<GameObject>(); //存储所有子菜单 public void StoreS ...

  4. 软件测试职业规划的思考(转)(作者Findyou

    前言 入软件测试行至今已经8年多,承领导们的信任与重用,同事的支持与信任,我的职业发展算是相对较好,从入行到各类测试技术岗位,再到测试总监,每一步都刚刚好.最近在自身职业发展瓶颈,人生十字路口,静坐反 ...

  5. Cracking The Coding Interview4.5

    //原文: // // Write an algorithm to find the 'next' node (i.e., in-order successor) of a given node in ...

  6. 第二节 java流程控制(判断结构+选择结构)

    Java的判断结构: 1.if(条件表达式){ 执行语句 }: 2.if(条件表达式){ 执行语句 }else{ 执行语句 } 3. if(条件表达式){ 执行语句 }else if(条件表达式){ ...

  7. java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 解决方法

    1.导入mysql-connector-java-5.1.26-bin.jar包,我试着把maven中自动下载下来的mysql-connector-java-5.1.26.jar包导入,还是没能解决问 ...

  8. DataTable数据统计方法

    调用方法: public object Compute(string strExpression,string strFilter) 参数说明: strExpression:要计算的表达式字符串,基本 ...

  9. linux的python版本升级

    可利用Linux自带下载工具wget下载,如下所示:     # wget http://www.python.org/ftp/python/2.7.3/Python-2.7.13.tgz 下载完成后 ...

  10. jenkins + nodejs + git 自动化部署前端

    1.  创建自定义风格任务 2.填写项目描述 3.配置源码管理 4. 系统管理->插件管理 ->安装插件 5.配置系统管理->全局工具配置-> 6.配置全局 ssh 7. 继续 ...