【HDU5723 2016 Multi-University Training Contest 1A】【最小生成树 + 简单算贡献】 Abandoned country 最小生成树的链长之和

来源:互联网 发布:topcoder算法 编辑:程序博客网 时间:2024/06/18 18:50

Abandoned country

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3451    Accepted Submission(s): 846


Problem Description
An abandoned country has n(n100000) villages which are numbered from 1 to n. Since abandoned for a long time, the roads need to be re-built. There are m(m1000000) roads to be re-built, the length of each road is wi(wi1000000). Guaranteed that any two wi are different. The roads made all the villages connected directly or indirectly before destroyed. Every road will cost the same value of its length to rebuild. The king wants to use the minimum cost to make all the villages connected with each other directly or indirectly. After the roads are re-built, the king asks a men as messenger. The king will select any two different points as starting point or the destination with the same probability. Now the king asks you to tell him the minimum cost and the minimum expectations length the messenger will walk.
 

Input
The first line contains an integer T(T10) which indicates the number of test cases. 

For each test case, the first line contains two integers n,m indicate the number of villages and the number of roads to be re-built. Next m lines, each line have three number i,j,wi, the length of a road connecting the village i and the village j is wi.
 

Output
output the minimum cost and minimum Expectations with two decimal places. They separated by a space.
 

Sample Input
14 61 2 12 3 23 4 34 1 41 3 52 4 6
 

Sample Output
6 3.33
 

Author
HIT
 

Source
2016 Multi-University Training Contest 1


#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 1e6+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;int casenum, casei;int n, m;struct A{int x, y, z;bool operator < (const A&b)const{return z < b.z;}}a[N];vector< pair<int,int> >b[N];int f[N];int find(int x){return f[x] == x ? f[x] : f[x] = find(f[x]);}int son[N];LL ans1;double ans2;void dfs(int x, int fa){son[x] = 1;for (int i = b[x].size() - 1; ~i; --i){int y = b[x][i].first;if (y == fa)continue;dfs(y,x);son[x] += son[y];ans2 += (double)son[y] * (n - son[y])*b[x][i].second;}}int main(){scanf("%d", &casenum);for (casei = 1; casei <= casenum; ++casei){scanf("%d%d", &n, &m);double K = (LL)n*(n - 1) / 2;for (int i = 1; i <= n; ++i){f[i] = i;b[i].clear();}for (int i = 1; i <= m; ++i){scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z);} sort(a + 1, a + m + 1);ans1 = ans2 = 0;for (int i = 1; i <= m; ++i){int x = a[i].x;int y = a[i].y;int z = a[i].z;int fx = find(x);int fy = find(y);if (fx != fy){f[fy] = fx;ans1 += z;b[x].push_back(MP(y, z));b[y].push_back(MP(x, z));}}dfs(1, 0);printf("%lld %.2f\n", ans1, ans2 / K);}return 0;}/*【trick&&吐槽】我是sb,明明小心过爆int,结果还是在n*n(-1)的乘法爆int了>.<【题意】n点m边(每边权值都不同)在求出最小生成树之后,让你算出对于C(n,2)条链的长度之和【类型】最小生成树+贡献计算【分析】如果每边权值都不同,那么最小生成树是否唯一?答案是唯一的。在这个基础上,我们直接求最小生成树,并算贡献即可【时间复杂度&&优化】O(mlogm)*/


0 0
原创粉丝点击