HDU_5723_最小生成树+任意两点距离的期望

Abandoned country

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

Problem DescriptionAn abandoned country has n(n≤100000)villages which are numbered from 1 to n . Since abandoned for a long time, the roads need to be re-built. There are
m(m≤1000000) roads to be re-built, the length of each road is wi(wi≤1000000) . Guaranteed that any two w 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.
 
InputThe first line contains an integer T(T≤10) 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,w , the length of a road connecting the village i and the village j is w .
 
Output
output the minimum cost and minimum Expectations with
two decimal places. They separated by a space.
 
Sample Input
1
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6
 

题意:求最小生成树,再求任意两点距离的期望。

最小生成树用kruscal。期望一开始不会求,看题解发现使用dfs:分别求每条边的贡献,一条边的贡献等于这条边的|左子树|*|右子树|*value。

代码中使用pair和vector

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std; struct Path
{
int x,y;
double value;
} path[]; typedef pair<int ,int> P;
vector <P> edge[]; bool cmp(Path a,Path b)
{
return a.value<b.value;
}
int father[],n,m;; int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
} void merge(int a,int b)
{
int x=find(a);
int y=find(b);
if(x!=y)
father[x]=y;
} long long sumv=,sumd=;
void kruscal()
{
for(int i=; i<=n; i++)
father[i]=i;
for(int i=; i<=n; i++)
edge[i].clear();
sort(path,path+m,cmp);
for(int i=; i<m; i++)
{
int x=path[i].x,y=path[i].y,value=path[i].value;
if(find(path[i].x)!=find(path[i].y))
{
sumv+=path[i].value;
merge(path[i].x,path[i].y);
edge[x].push_back(P(y,value));
edge[y].push_back(P(x,value));
}
}
} int dfs(int x,int last)
{
int cnt=;
for(int i=;i<edge[x].size();i++)
{
int y=edge[x][i].first,value=edge[x][i].second;
if(last!=y)
{
int now=dfs(y,x);
cnt+=now;
sumd+=1.0*now*(n-now)*value;
}
}
return cnt+;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
sumv=;
sumd=;
scanf("%d%d",&n,&m);
for(int i=; i<m; i++)
{
scanf("%d%d%lf",&path[i].x,&path[i].y,&path[i].value);
}
kruscal();
dfs(,-);
printf("%I64d %.2lf\n",sumv,sumd*2.0/(1.0*n)/(n-1.0));
}
return ;
}
Sample Output
6 3.33
 
Author
HIT
 
Source
上一篇:Unity3D 记第一次面试


下一篇:基于Unity3D 的Vuforia SDK开发基础教程