HDU 5723 Abandoned country (最小生成树 + 期望值)

来源:互联网 发布:linux 原生 迅雷 64 编辑:程序博客网 时间:2024/06/05 20:09

Abandoned country

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


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 * (n - 1) ,所有路径 = sum(边的权值 * 该边用到的次数)。每条边用到的次数等于该边左边的端点个数 * 右边的端点个数,用深搜可以在O(n)复杂度求出。

代码如下:

<strong>#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<algorithm>using namespace std;struct edge{int u, v, cost;}es[1000005];//储存边的信息 struct node{int vv;int cc;};//点 vector<node>g[100005];//邻接表构图 int n, m;int father[100005];double ans;bool cmp(edge n1, edge n2){return n1.cost < n2.cost;}void init()//初始化 {int i;for(i = 0; i <= 100004; i++)g[i].clear();for(i = 0; i <= 100004; i++){father[i] = i;}}int find_set(int vex)//并查集查找根节点 {if(vex == father[vex])return vex;elsereturn father[vex] = find_set(father[vex]);//路径压缩 }long long kruskal()//克鲁斯卡尔算法求最小生成树 {long long sum = 0;int i, node_num = 0;for(i = 0; i < m; i++){int a = find_set(es[i].u);int b = find_set(es[i].v);if(a == b)continue;//若两个节点同根,则加入该边会导致成环 else{sum += es[i].cost;father[a] = b;node_num++;node n1, n2;n1.vv = es[i].u; n1.cc = es[i].cost; n2.vv = es[i].v; n2.cc = es[i].cost;g[es[i].u].push_back(n2);//重构最小生成树g[es[i].v].push_back(n1);}if(node_num == n-1)break;}return sum;}int dfs(int u, int pre)//深搜求每条边用到的 次数 *长度 { int cnt = 0;for(int i = 0; i < g[u].size(); i++){int v = g[u][i].vv;if(v != pre){int temp = dfs(v,u);cnt += temp;ans = ans + 1.0*temp * (n - temp) * g[u][i].cc;}}return cnt + 1; }int main(){int cases, i, j;scanf("%d", &cases);while(cases--){init();scanf("%d%d", &n, &m);for(i = 0; i < m; i++){scanf("%d%d%d", &es[i].u, &es[i].v, &es[i].cost);}sort(es, es + m, cmp);long long sum = kruskal();ans = 0;int x = dfs(1, -1);ans = ans * 2.0 / (n * 1.0) / (n - 1.0);printf("%I64d %.2f\n", sum, ans);}return 0;}</strong>


0 0
原创粉丝点击