hdu 5723 Abandoned country

来源:互联网 发布:桔子酒店 知乎 编辑:程序博客网 时间:2024/04/29 10:25

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



一开始想生成最小生成树后树DP的,后来发现只要统计每条边对答案的贡献就好了。

每条边的贡献是两边节点数相乘

最后用这个值乘2再除以n(n+1)就可以得到结果

#include<cstdio>#include<string>#include<cstring>#include<algorithm>#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std;struct line{int s,t;double x;int next;bool operator <(line y) const{return x<y.x;}}a[200001],x[1000001];int head[100001];int edge;inline void add(int s,int t,double x){a[edge].next=head[s];head[s]=edge;a[edge].s=s;a[edge].t=t;a[edge].x=x;}int fa[100001];double son[100001];inline int find(int x){if(x!=fa[x])fa[x]=find(fa[x]);return fa[x];}inline void dfs(int d){son[d]=0;int i;for(i=head[d];i!=0;i=a[i].next){int t=a[i].t;if(fa[d]!=t){fa[t]=d;dfs(t);son[d]+=son[t]+1;}}}int main(){int T;scanf("%d",&T);while(T>0){T--;int n,m;scanf("%d%d",&n,&m);int i;for(i=1;i<=m;i++)scanf("%d%d%lf",&x[i].s,&x[i].t,&x[i].x);sort(x+1,x+1+m);edge=0;memset(head,0,sizeof(head));for(i=1;i<=n;i++)fa[i]=i;double sx=0;int ss=0;for(i=1;i<=m;i++){int fx=find(x[i].s),fy=find(x[i].t);if(fx!=fy){ss++;fa[fx]=fy;edge++;add(x[i].s,x[i].t,x[i].x);edge++;add(x[i].t,x[i].s,x[i].x);sx+=x[i].x;if(ss==n-1)break;}}memset(fa,0,sizeof(fa));dfs(1);double sum=0;for(i=1;i<=edge;i++){int s=a[i].s,t=a[i].t;if(fa[s]==t)sum+=a[i].x*(double)(son[1]-son[s])*(double)(son[s]+1);}printf("%.0f %.2f\n",sx,sum*2.0/(double)(n)/(double)(n-1));}return 0;}


0 0