HDU 5723 Abandoned country (并查集 + DFS+求解树上任意两点间的距离的平均值)

来源:互联网 发布:数据产品运营专员 编辑:程序博客网 时间:2024/06/09 19:55

Abandoned country

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


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
 

题意:给你n个顶点和m条边,求出他的最小生成树的值(树是唯一的),同时求出两两节点之间距离的数学期望
先用kruskal算法在求出最小生成树,保存这条最小生成树两两连接的边,然后用DFS遍历这棵树,通过记录一条边被访问了多少次来得到最终的数学期望:
算法:已知一个节点和他的父亲节点,那么我们可以求出这个节点的所有子树节点数x,然后我们就可以通过x * (n-x)*cost来求出这个节点和他的父亲结点连接的那条边被访问了多少次,及他们的花费,接着一一求得即可。
由于我的代码中只有节点为起点,但是一个节点即可做终点也可以做起点,所以最终的值要乘以2
#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <vector>#include <algorithm>using namespace std;const int MAXN = 1e6 + 5;typedef long long LL;struct edge {int v, u, cost;bool operator <(const edge &a) const {return cost < a.cost;}} es[MAXN];struct o {int v, cost;o() {}o(int v, int cost):v(v), cost(cost) {}};int par[MAXN], ranks[MAXN];int sum[MAXN];bool vis[MAXN];int n, m;double minres;vector<o>G[MAXN];void init(int sizes) {//初始化for(int i = 0; i <= sizes; i ++) {par[i] = i;ranks[i] = 1;G[i].clear();}}int find(int x) {return par[x] == x ? x : par[x] = find(par[x]);}bool same(int x,int y) {return find(x) == find(y);}void unite(int x,int y) {//连接x = find(x);y = find(y);if(x == y)return ;if(ranks[x] > ranks[y]) {par[y] = x;} else {par[x] = y;if(ranks[x] == ranks[y]) ranks[x] ++;}}LL kruskal() {//最小生成树算法sort(es,es + m);init(n);LL ret = 0;for(int i=0; i<m; i++) {edge e = es[i];if(!same(e.u, e.v)) {unite(e.u, e.v);ret += e.cost;G[e.u].push_back(o(e.v, e.cost));//将生成树中对应的点和边的值保存起来G[e.v].push_back(o(e.u, e.cost));}}return ret;}void DFS(int r, int f, int cost) {//得到最终的数学期望if(vis[r]) return;vis[r] = true;sum[r] = 1;for(int i = 0; i < G[r].size(); i ++) {if(G[r][i].v == f) continue;DFS(G[r][i].v, r, G[r][i].cost);sum[r] += sum[G[r][i].v];}if(cost != -1)minres += (LL)sum[r] * (LL)(n - sum[r]) * (LL)cost * 1.;//x * (n - 1) * cost即这条边的花费}int T;int main() {scanf("%d", &T);while(T --) {scanf("%d%d", &n, &m);for(int i = 0; i < m; i ++) {scanf("%d%d%d",&es[i].v, &es[i].u, &es[i].cost);}if(n == 1){            printf("0 0.00\n");            continue;}LL minret = kruskal();memset(sum, 0, sizeof(sum));memset(vis, false, sizeof(vis));minres = 0;DFS(1, 1, -1);printf("%I64d %.2lf\n", minret, minres * 2./ ((LL)n * (n - 1)));}return 0;}




1 0