HDU 5176 The Experience of Love(带权并查集)
来源:互联网 发布:linux 复制目录及文件 编辑:程序博客网 时间:2024/05/16 13:49
The Experience of Love
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 309 Accepted Submission(s): 119
Problem Description
A girl named Gorwin and a boy named Vivin is a couple. They arrived at a country named LOVE. The country consisting of N cities and only N−1 edges (just like a tree), every edge has a value means the distance of two cities. They select two cities to live,Gorwin living in a city and Vivin living in another. First date, Gorwin go to visit Vivin, she would write down the longest edge on this path(maxValue).Second date, Vivin go to Gorwin, he would write down the shortest edge on this path(minValue),then calculate the result of maxValue subtracts minValue as the experience of love, and then reselect two cities to live and calculate new experience of love, repeat again and again.
Please help them to calculate the sum of all experience of love after they have selected all cases.
Please help them to calculate the sum of all experience of love after they have selected all cases.
Input
There will be about 5 cases in the input file.
For each test case the first line is a integerN , Then follows n−1 lines, each line contains three integers a , b , and c , indicating there is a edge connects city a and city b with distance c .
[Technical Specification]
1<N<=150000,1<=a,b<=n,1<=c<=109
For each test case the first line is a integer
[Technical Specification]
Output
For each case,the output should occupies exactly one line. The output format is Case #x: answer, here x is the data number, answer is the sum of experience of love.
Sample Input
31 2 12 3 251 2 22 3 52 4 73 5 4
Sample Output
Case #1: 1Case #2: 17Hinthuge input,fast IO method is recommended.In the first sample:The maxValue is 1 and minValue is 1 when they select city 1 and city 2, the experience of love is 0.The maxValue is 2 and minValue is 2 when they select city 2 and city 3, the experience of love is 0.The maxValue is 2 and minValue is 1 when they select city 1 and city 3, the experience of love is 1.so the sum of all experience is 1;
Source
Valentine's Day Round
题意:给一棵树,求任意{两点路径上的最大边权值-最小边权值}的总和。
program:解法:
sigma(maxVal[i]-minVal[i]) = sigma(maxVal)-sigma(minVal);所以我们分别求所有两点路径上的最大值的和,还有最小值的和。再相减就可以了。求最大值的和的方法用带权并查集,把边按权值从小到大排序,一条边一条边的算,当我们算第i 条边的时候权值为wi ,两点是ui,vi ,前面加入的边权值一定是小于等于当前wi 的,假设与ui 连通的点有a 个,与vi 连通的点有b 个,那么在a 个中选一个,在b 个中选一个,这两个点的路径上最大值一定是wi ,一共有a∗b 个选法,爱情经验值为a∗b∗wi 。求最小值的和的方法类似。
#include<stdio.h>#include<algorithm>using namespace std;typedef unsigned long long ULL;#define N 200000int p[N];ULL r[N];struct Edge{ int u, v; ULL w;}e[N];bool cmp(Edge c, Edge d) {return c.w < d.w;}int find(int x) {return p[x] == x ? x : p[x]=find(p[x]);}int main(){ int n, t = 1; while(~scanf("%d", &n)) { int m = n-1; for(int i=1; i<=m; i++) scanf("%d%d%llu", &e[i].u,&e[i].v,&e[i].w); sort(e+1,e+m+1,cmp); for(int i=1; i<=n; i++) p[i] = i, r[i] = 1; ULL res = 0; for(int i=1; i<=m; i++) { int x = find(e[i].u); int y = find(e[i].v); res += r[x]*r[y]*e[i].w; p[y] = x; //x是始祖 r[x] += r[y]; } for(int i=1; i<=n; i++) p[i] = i, r[i] = 1; for(int i=m; i>=1; i--) { int x = find(e[i].u); int y = find(e[i].v); res -= r[x]*r[y]*e[i].w; p[y] = x; r[x] += r[y]; } printf("Case #%d: %llu\n", t++,res); } return 0;}
0 0
- HDU 5176 The Experience of Love(带权并查集)
- hdu 5176 The Experience of Love(并查集)
- hdu 5176 The Experience of Love(带权并查集+思维)
- HDU 5176 The Experience of Love (带权并查集 + 贪心)
- 【并查集】 HDOJ 5176 The Experience of Love
- hdu5176 The Experience of Love[树 并查集]
- hdu 5176 The Experience of Love
- HUD 5176 The Experience of Love
- hdu5176---The Experience of Love
- 并查集--HDU 3974Assign the task--带权
- 带权并查集(hdu 3635,hdu 2818)
- hdu 2818 带权并查集
- hdu 3047 带权并查集
- hdu 3635 带权并查集
- HDU 3635 带权并查集
- hdu 3038带权并查集
- hdu-3038 带权并查集
- HDU 3047 带权并查集
- PAT 1080 Graduate Admission
- PAT (Basic Level) Practise (中文) 1026. 程序运行时间(15)
- codeforces 107C Arrangement (状压dp)
- 【Uva 232】 Crossword Answers
- POJ 3169 Layout(差分约束基础)
- HDU 5176 The Experience of Love(带权并查集)
- 第三方应用分享到微信朋友圈功能
- LPC2138编程时出现错误“Timer.sct(7): error: L6236E: No section matches selector - no section to be FIRST/LAS
- java软件复杂运算,同时显示进度条的一种方法
- 一款桌面英语翻译软件介绍
- Oracle SQL*Loader
- Linux的ACL权限
- Leetcode:Path Sum 菜鸟解法
- hdoj 1874 通畅工程续 (最短路)