题目链接: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的更多相关文章

  1. 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 ...

  2. ZOJ 3659 & HDU 4424 Conquer a New Region (并查集)

    这题要用到一点贪心的思想,因为一个点到另一个点的运载能力决定于其间的边的最小权值,所以先把线段按权值从大到小排个序,每次加的边都比以前小,然后合并集合时,比较 x = findset(a) 做根或 y ...

  3. zoj 3659 Conquer a New Region

    // 给你一颗树 选一个点,从这个点出发到其它所有点的权值和最大// i 到 j的最大权值为 i到j所经历的树边容量的最小值// 第一感觉是树上的dp// 后面发现不可以// 看了题解说是并查集// ...

  4. zoj 3659 Conquer a New Region(并查集)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...

  5. zoj 3659 并检查集合

    http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4882 现在在牡丹江,明天regional直播比赛,我会在一个月内退休.求祝福 ...

  6. 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 ...

  7. zoj 3659 第37届ACM/ICPC 长春赛区现场赛E题 (并查集)

    题意:给出一棵树,找出一个点,求出所有点到这个点的权值和最大,权值为路径上所有边权的最小值. 用神奇的并查集,把路按照权值从大到小排序,然后用类似Kruskal的方法不断的加入边. 对于要加入的一条路 ...

  8. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  9. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

随机推荐

  1. java String对象的创建(jvm).

    本人目前也开始学习虚拟机,在java中,有很多种类型的虚拟机,其中就以sum公司(当然现在已经是oracle了)的虚拟机为例,介绍可能在面试的时候用到的,同时对自己了解String有很大帮助,这里仅仅 ...

  2. [转帖]FPGA--Vivado

    来源:http://home.eeworld.com.cn/my/space-uid-639749-blogid-267593.html 一般的,在Verilog中最常用的编码方式有二进制编码(Bin ...

  3. Spring 中的注解

    1.普通方式注解 a.在配置文件中配置 1.导入命名空间              xmlns:context="http://www.springframework.org/schema/ ...

  4. DX笔记之一---Direct3D基础

    一.预备知识 1.表面 表面就是Direct3D用于储存2D图像数据的一个像素矩阵.width和height以像素为单位,pitch以字节单位,用接口IDirect3DSurface来描述表面 Loc ...

  5. SGU 143.Long Live the Queen(女王万岁)

    时间限制:0.25s 空间限制:4M 题意: 有n(n<=16000)个小镇,每两个小镇有且仅有一条路径相连.每个小镇有一个收益x(-1000<=x<=1000). 现在要求,选择一 ...

  6. WebDriverWait 中 and, or, not用法

    1. And 用法 wait.until(ExpectedConditions.and( ExpectedConditions.visibilityOfAllElementsLocatedBy(By. ...

  7. [转载]python os.path模块

    os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.ht ...

  8. js文字向上滚动代码

    js文字向上滚动代码 <style>.pczt_pingfen_jhxs_news1{ width:397px;  background:#edfafd; padding-top:2px; ...

  9. magento错误 Service Temporarily Unavailable magento

    前台访问出现错误 Service Temporarily Unavailable magento 解决方法 Service TemporarilyUnavailable字面意思是此服务暂时无法使用,如 ...

  10. 安卓SDK Manager自动管理各种包

    安卓ADT不能自动从google下载sdk等各种工具 修改hosts文件下载成功 210.242.125.89  dl-ssl.google.com 210.242.125.89  dl.google ...