UVa 1664 Conquer a New Region(并查集)
https://vjudge.net/problem/UVA-1664
题意:
n个城市形成一棵树,每条边有权值C(i,j)。任意两个点的容量S(i,j)定义为i与j唯一通路上容量的最小值。找一个点,使得它到其他所有点的容量之和最大。
思路:
做法有点类似于最小生成树的Kruskal算法。
先将边按权值从大到小排列,每次新加入一条边,检查边两段A和B所处并查集的根结点,并通过计算得出谁作为中心点时容量更大。计算过程中需要维护一些东西,sum[i]是以i为中心时的容量之和,cnt[i]是以i为根结点时树的结点个数(初始时都是为1)。
下面举例说明:
1 2 2
2 4 1
2 3 1
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std; const int maxn = + ; struct node
{
int u, v;
int w;
}t[maxn]; int p[maxn];
long long sum[maxn];
long long cnt[maxn];
int n;
long long ans; bool cmp(node a, node b)
{
return a.w > b.w;
} int find(int x)
{
return x == p[x] ? x : find(p[x]);
} void solve()
{
ans = ;
for (int i = ; i <= n; i++) { p[i] = i; sum[i] = ; cnt[i] = ; }
sort(t, t + n - , cmp);
for (int i = ; i < n - ; i++)
{
int x = find(t[i].u);
int y = find(t[i].v);
long long sum1 = sum[x] + cnt[y] * t[i].w;
long long sum2 = sum[y] + cnt[x] * t[i].w;
if (sum1>sum2)
{
p[y] = x;
cnt[x] += cnt[y];
sum[x] = sum1;
ans = sum[x];
}
else
{
p[x] = y;
cnt[y] += cnt[x];
sum[y] = sum2;
ans = sum[y];
}
}
printf("%lld\n", ans);
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
while (~scanf("%d", &n))
{
for (int i = ; i < n - ; i++)
scanf("%d%d%d", &t[i].u, &t[i].v, &t[i].w);
solve();
}
return ;
}
UVa 1664 Conquer a New Region(并查集)的更多相关文章
- UVA 1664 Conquer a New Region (并查集+贪心)
并查集的一道比较考想法的题 题意:给你n个点,接着给你n-1条边形成一颗生成树,每条边都有一个权值.求的是以一个点作为特殊点,并求出从此点出发到其他每个点的条件边权的总和最大,条件边权就是:起点到终点 ...
- UVA 1664 Conquer a New Region (Kruskal,贪心)
题意:在一颗树上要求一个到其他结点容量和最大的点,i,j之前的容量定义为i到j的路径上的最小边容量. 一开始想过由小到大的去分割边,但是很难实现,其实换个顺序就很容易做了,类似kruskal的一个贪心 ...
- 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 ...
- hdu4424 Conquer a New Region 并查集/类似最小生成树
The wheel of the history rolling forward, our king conquered a new region in a distant continent.The ...
- 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 ...
- UVA 1395 苗条的生成树(最小生成树+并查集)
苗条的生成树 紫书P358 这题最后坑了我20分钟,怎么想都对了啊,为什么就wa了呢,最后才发现,是并查集的编号搞错了. 题目编号从1开始,我并查集编号从0开始 = = 图论这种题真的要记住啊!!题目 ...
随机推荐
- sql语句查询条件的不同表达方式对查询性能的影响
今天操作数据库遇到一个问题 目标表RA_AD_DAILY_DATA的数据量大概有5千万左右,其中的BUSINESS_DATE字段为日期类型 我要查询8月20号导入的三条记录,刚开始用这种方式去查: S ...
- 知道WCF的地址用工厂通道方式快速调用WCF
知道WCF的地址用工厂通道方式快速调用WCF 1 using System; 2 using System.ServiceModel; 3 using System.ServiceModel.D ...
- expdp全库备份rac数据库因错误终止
1.expdp导出日志报错如下: ORA-39014: One or more workers have prematurely exited. ORA-39029: worker 2 with pr ...
- console access jquery--------json
jq = document.createElement('script'); jq.src = "file:///home/liulqiang/jquery.js"; docu ...
- Ultra-QuickSort---poj2299 (归并排序.逆序数.树状数组.离散化)
题目链接:http://poj.org/problem?id=2299 题意就是求把数组按从小到大的顺序排列,每次只能交换相邻的两个数, 求至少交换了几次 就是求逆序数 #include<std ...
- [GDAL]编译64位GDAL1.10
环境VS2010,swigwin-2.0.11 1. 打开nmake.opt文件,找到SWIG=swig.exe这一句,假如没有将swig的目录添加到环境变量中,那么将这句后面的swig.exe修改为 ...
- 【转】Deep Learning(深度学习)学习笔记整理系列之(四)
九.Deep Learning的常用模型或者方法 9.1.AutoEncoder自动编码器 Deep Learning最简单的一种方法是利用人工神经网络的特点,人工神经网络(ANN)本身就是具有层次结 ...
- curl获取远程文件内容
curl获取远程文件内容 ** 获取远程文件内容 @param $url 文件http地址 * function fopen_url($url) { if (function_exists(& ...
- activiti 数据表设计
activiti数据表分为5个部分: 通用数据表.流程存储表.身份数据表.运行时数据表.历史数据表 1.通用(general)数据表 以ACT_GE开头 资源表-act_ge_btyearray: 用 ...
- And Design:拓荒笔记——Form表单
And Design:拓荒笔记——Form表单 Form.create(options) Form.create()可以对包含Form表单的组件进行改造升级,会返回一个新的react组件. 经 For ...