hdu4424 Conquer a New Region 并查集/类似最小生成树
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 并查集/类似最小生成树的更多相关文章
- 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 ...
- ZOJ3659 Conquer a New Region 并查集
Conquer a New Region Time Limit: 5 Seconds Memory Limit: 32768 KB The wheel of the history roll ...
- ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)
这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...
- zoj 3659 Conquer a New Region(并查集)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...
- hdu 4424 Conquer a New Region (并查集)
///题意:给出一棵树.树的边上都有边权值,求从一点出发的权值和最大,权值为从一点出去路径上边权的最小值 # include <stdio.h> # include <algorit ...
- POJ-2421Constructing Roads,又是最小生成树,和第八届河南省赛的引水工程惊人的相似,并查集与最小生成树的灵活与能用,水过~~~
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Description There are N v ...
- hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)
还是畅通project Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdu 1233 还是畅通工程 并查集or最小生成树
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路 ...
- POJ 3723 Conscription (Kruskal并查集求最小生成树)
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14661 Accepted: 5102 Des ...
随机推荐
- Daily record-August
August11. A guide dog can guide a blind person. 导盲犬能给盲人引路.2. A guide dog is a dog especially trained ...
- POST提交表单时EnType设置问题
POST提交表单时EnType设置问题 首先知道enctype这个属性管理的是表单的MIME编码.共有三个值可选: 1.application/x-www-form-urlencoded 2.mult ...
- :工厂模式2:抽象工厂模式--Pizza
#ifndef __INGREDIENT_H__ #define __INGREDIENT_H__ #include <iostream> using namespace std; cla ...
- VS2010编译Unigine_2010源码
VS2010编译Unigine_2010源码[Debug版本] 1.Laucher工程属性改为控制台项目 2.Unigine工程编译时的Warnning LNK2019 a.属性--常规-目标文件名改 ...
- ubuntu gnome桌面隐藏顶栏
注意:ubuntu 14.04.5默认的为unity桌面,有多点触发,没有自带Tweak Tool工具.需安装gnome 桌面,可参见我的另一随笔. 环境: ubuntu 14.04.5 gnome ...
- 1043 输出PATest
给定一个长度不超过 104 的.仅由英文字母构成的字符串.请将字符重新调整顺序,按 PATestPATest.... 这样的顺序输出,并忽略其它字符.当然,六种字符的个数不一定是一样多的,若某种 ...
- 卷积神经网络-Dropout
dropout 是神经网络用来防止过拟合的一种方法,很简单,但是很实用. 基本思想是以一定概率放弃被激活的神经元,使得模型更健壮,相当于放弃一些特征,这使得模型不过分依赖于某些特征,即使这些特征是真实 ...
- get 和post 请求的写法
get请求 import requests base_url = 'http://httpbin.org' # 定义请求所需的参数,参数之间以英文逗号隔开 param_data = {'} # 发送G ...
- PMS5003ST传感器
5003ST传感器已经收到,准备开始DIY PM2.5检测仪 一款可以同时监测空气中颗粒物浓度.甲醛浓度及温湿度的三 合一传感器.其中颗粒物浓度的监测基于激光散射原理,可连续采集并计算单位 ...
- Python 深度递归
class A: pass class B(A): pass class C(A): pass class D(B, C): pass class E: pass class F(D, E): pas ...