题目链接: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. install erlang environment on centos

    #(erlide in linux can't detect the runtime if build from source, but erlang shell works correctly)su ...

  2. solve_lock-1024-大功告成

    create or replace procedure solve_lock_061203(v_msg out varchar2) as  v_sql varchar2(3000); --定义 v_s ...

  3. js获取标签的几种方式

    一:id获取(全部浏览器兼容) document.getElementById(""); <body> <div id="box">&l ...

  4. 绘图quartz之渐变

    实现线性渐变   径向渐变   自己新建的view中有一个drawRect:(cgrect)rect方法 在这个方法里 可以通过画图 将内容显示在画板上(即最下边的view)   渐变的方式分两种   ...

  5. 获取C++类成员变量的地址偏移

    今天有在校学生问怎么获取类中的成员变量的地址偏移量,这个应该是很多初学C++的人很好奇的问题.以前我在学校的时候,也有过这种需求.忘了当时是要写什么“奇怪的程序”了,反正需要获取一个类的成员变量的地址 ...

  6. 【POJ1568】【极大极小搜索+alpha-beta剪枝】Find the Winning Move

    Description 4x4 tic-tac-toe is played on a board with four rows (numbered 0 to 3 from top to bottom) ...

  7. 用jq 做了一个排序

    <ul id="cont"> <li data="5">5</li> <li data="1"&g ...

  8. BIOS中断大全

    BIOS中断大全 BIOS中断:1.显示服务(Video Service——INT 10H)  00H —设置显示器模式0CH —写图形象素01H —设置光标形状0DH —读图形象素02H —设置光标 ...

  9. H5中需要掌握的 ANIMATION 动画效果

    CSS3的动画在PC网页上或者APP上用得越来越多,比如H5页面的应用,目前在营销传播上的意义比较大,还有企业官网或者APP主要介绍也用得比较多,当然还有很多地方都用到.所以学习css的动画也迫在眉睫 ...

  10. php基础知识【函数】(2)文件file

    一.路径  1.dirname -- 返回路径中的目录部分      $path = "/etc/passwd";      $file = dirname($path); // ...