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 ...
随机推荐
- javascript ~~ 符号的使用
其实是一种利用符号进行的类型转换,转换成数字类型 大概是这样滴: ~~true == 1 ~~false == 0 ~~"" == 0 ~~[] == 0 ~~undefined ...
- JavaScript学习笔记--ES6学习(五) 数值的扩展
ES6 对于数值类型 (Number) 进行了一下扩展: 1.对于二进制和八进制提供了新的写法 ES6对于二进制和八进制的数值提供了新的写法,分别用0b (或者0B) 和0o (或者0o) 表示.例如 ...
- 封装JDBC:实现简单ORM框架lfdb
作者:Vinkn 来自http://www.cnblogs.com/Vinkn/ 一.简介 框架就是一组可重用的构件,LZ自己写的姑且就叫微型小框架:lfdb.LZ也对其他的ORM框架没有什么了解,现 ...
- [转]iframe自适应宽度高度
<iframe id="iframe" onLoad="AutoFit();" frameborder="0" scrolling=& ...
- jQuery--效果和遍历
七.jQuery效果 (1)jQuery隐藏和显示 //隐藏 $("#hide").click(function(){ $("p").hide(1000); } ...
- 一站式远程页面调试工具spy-debugger 2.0,已支持HTTPS
项目名称: spy-debugger 项目地址:https://github.com/wuchangming/spy-debugger 关于spy-debugger npm Build Status ...
- openshif ssh proxy
最近google又被墙了.没办法 1:注册一个openshift账号.申请注册一个app,获取一个免费主机. https://www.openshift.com/ 2:去PuTTY官方网站下载pL ...
- HTML部分标签的含义(2)
1,ul标签,添加新闻信息列表 使用ul标签,信息无先后顺序 这些列表就可以用ul-li标签来完成 语法:<ul> <li>信息</li> <li>信息 ...
- Jenkins 快速搭建持续集成环境
持续集成概述 什么是持续集成 随着软件开发复杂度的不断提高,团队开发成员间如何更好地协同工作以确保软件开发的质量已经慢慢成为开发过程中不可回避的问题.尤其是近些年来,敏捷(Agile) 在软件工程领域 ...
- eclipse中tomcat内存溢出问题,报PermGen space
场景 最近在eclipse中的tomcat服务器下放三个不同的应用程序,其中两个应用程序用到了各自的第三方jar包.刚开始时把这三个应用程序分别部署到各自的tomcat服务器运行,没问题.后来想通过第 ...