zoj 3659
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3659
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std; const int maxn = ;
int p[maxn],num[maxn];
long long int sum[maxn];
struct Edge{
int u,v,c;
bool operator <(const Edge & rh) const{
return c > rh.c;
}
};
vector<Edge> e;
int n; int find(int x){
return x == p[x] ? x : p[x] = find(p[x]);
}
void init(){
e.clear();
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++){
p[i] = i;
num[i] = ;
}
}
int main()
{
//if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);} while(cin>>n){
init();
for(int i=;i<n;i++){
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
e.push_back((Edge){u,v,c});
}
sort(e.begin(),e.end());
for(int i=;i<n-;i++){
int a = find(e[i].u), b = find(e[i].v);
long long int suma,sumb;
suma = sum[a] + (long long)num[b]*e[i].c ;
sumb = sum[b] + (long long)num[a]*e[i].c ;
if(suma >= sumb) {p[b] = a; sum[a] = suma; num[a] = num[a] + num[b];}
else {p[a] = b; sum[b] = sumb; num[b] = num[a] + num[b];}
//printf("%d %d %d %d\n",a,b,suma,sumb);
}
printf("%lld\n",sum[find()]);
}
}
//wa的原因就在于long long没使用正确;
//本题巧妙的用并查集,归纳递推;
zoj 3659的更多相关文章
- 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 ...
- ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)
这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...
- zoj 3659 Conquer a New Region
// 给你一颗树 选一个点,从这个点出发到其它所有点的权值和最大// i 到 j的最大权值为 i到j所经历的树边容量的最小值// 第一感觉是树上的dp// 后面发现不可以// 看了题解说是并查集// ...
- zoj 3659 Conquer a New Region(并查集)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...
- zoj 3659 并检查集合
http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4882 现在在牡丹江,明天regional直播比赛,我会在一个月内退休.求祝福 ...
- zoj 3659 Conquer a New Region The 2012 ACM-ICPC Asia Changchun Regional Contest
Conquer a New Region Time Limit: 5 Seconds Memory Limit: 32768 KB The wheel of the history roll ...
- zoj 3659 第37届ACM/ICPC 长春赛区现场赛E题 (并查集)
题意:给出一棵树,找出一个点,求出所有点到这个点的权值和最大,权值为路径上所有边权的最小值. 用神奇的并查集,把路按照权值从大到小排序,然后用类似Kruskal的方法不断的加入边. 对于要加入的一条路 ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
随机推荐
- log4Net配置详解
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSe ...
- 115个Java面试题和答案——终极列表
from http://www.importnew.com/10980.html#collection http://www.importnew.com/11028.html 下面的章节分为上下两篇, ...
- 大规模字符串检索-压缩trie树
本文使用压缩trie树实现字符串检索的功能.首先将字符串通过编码转化为二进制串,随后将二进制串插入到trie树中,在插入过程中同时实现压缩的功能. 字符编码采用Huffman,但最终测试发现不采用Hu ...
- Linq查询IEnumerable与IQueryable
class Program { static void Main(string[] args) { System.Diagnostics.Stopwatch stp = new Stopwatch() ...
- Eclipse 编译StanfordNLP
1.源码最新下载地址:http://nlp.stanford.edu/software/index.shtml; 2.解压stanford-corenlp.zip; 3.打开Eclipse新建JAVA ...
- 一个小白对Arguments的理解
1.Arguments是一个类似数组的实参队列,译为:论据,论点,姑且译作参数(实参): 2.Arguments与形参保持不同的存储空间,只有当形参被赋值之后两者才会相等 3.传说在严格模式下函数内自 ...
- Dev gridview 调整字体大小
//调整表头字体大小 this.gridView1.Appearance.HeaderPanel.Font = new Font("Tahoma", 20, FontStyle.R ...
- js文字向上滚动代码
js文字向上滚动代码 <style>.pczt_pingfen_jhxs_news1{ width:397px; background:#edfafd; padding-top:2px; ...
- pkg-config相关的常用指令
pkg-config用途: 查询系统已安装库的基础信息(元信息) 1.查看所有的pkg-config库 pkg-config --list-all --list-all 列出pkg-config路径 ...
- ios新特征 ARC详解
IOS ARC 分类: IOS ARC2013-01-17 09:16 2069人阅读 评论(0) 收藏 举报 目录(?)[+] 关闭工程的ARC(Automatic Reference Co ...