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 ...
随机推荐
- Win10系列:VC++调用自定义组件1
通过20.9.1小节中的代码和步骤编写了一个名为"FilePickerComponent"的WinRT组件,接下来将在上一小节所新建的项目基础上,继续介绍如何在不同的语言所编写的应 ...
- 网卡驱动-BD详解(缓存描述符 Buffer Description)
DMA介绍(BD的引入) 网络设备的核心处理模块是一个被称作 DMA(Direct Memory Access)的控制器,DMA 模块能够协助处理器处理数据收发.对于数据发送来说,它能够将组织好的数据 ...
- learning ddr mode register MR1
- python之路-python字符编码
编码的发展 py2的字符编码 py3的字符编码 编码的发展: 编码表:编码是信息从一种形式或格式转换为另一种形式的过程也称为计算机编程语言的代码简称编码.编码表记录着我们常见的明文所对应的计算机二进制 ...
- Python之路-python基础一
本章内容: 一.编程语言分类 二.python之变量介绍 三.python交互命令(input,print,getpass) 四.Python之循环控制 ...
- python全栈开发笔记-----------概要
Python开发 开发: 开发语言: 高级语言:python.Java.php .C# .Go .ruby . C++ .... ===>字节码 低级语言:C.汇编 ...
- 每天CSS学习之transform
transform是CSS3的一个属性,其作用是用来进行2D或3D变换. 一.2D变换 1. translate(x-offset , y-offset) translate的作用就是用作位置的移动. ...
- 【Ctrl】 + 【Alt】 + 【F1~F6】 和 【Ctrl】 + 【Alt】 + 【T】打开的终端有什么不同?
ctrl +alt +Fn 打开的是模拟终端,简单说来,Linux系统一开机会自动打开6个模拟终端,然后自动切换到其中一个(一般来说是切换到图形界面的那个也就是说窗口管理器是在这6个模拟终端中运行的) ...
- 第一篇 入门必备 (Android学习笔记)
第一篇 入门必备 第1章 初识Android 第2章 搭建你的开发环境 第3章 创建第一个程序--HelloWorld 第4章 使用Android工具 ●Android之父 Android安迪·罗 ...
- Linux3.10.0块IO子系统流程(4)-- 为请求构造SCSI命令
首先来看scsi_prep_fn int scsi_prep_fn(struct request_queue *q, struct request *req) { struct scsi_device ...