题目解析:最小生成树+树形dp
扫描二维码
随时随地手机看文章
题面:
Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1204 Accepted Submission(s): 315
Problem Description
An 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 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(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,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
1
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6
Sample Output
6 3.33
Author
HIT
Source
2016 Multi-University Training Contest 1
题意:
给定一张图,求最小生成树,并求在图中任取两点,两点间路径代价的期望值。
解题:
因为求路径代价都是唯一的,求两点间路径代价最小值,即求最小生成树上的路径最小值。代价是路径边上的值,故我们可以考虑最小生成树上的边,被取到的概率乘以其权值,累加边代价期望,即可得到总期望。而每条边被取到的概率为该边两侧的点数量的乘积除以C(n,2)。
先求最小生成树,并在寻找树的过程中,保留最小生成树上的边,用于后续计算期望。采用dfs的方式,任意从树上一点出发,计算该节点所在的子树上的节点数x,并由总数减去x得到边另一侧的节点数。
代码:
#include#include#include#include#include#include#include#include#include#include#include#include#define eps 1e-8 #define LL long long #define sz1 1000010 #define sz2 100010 using namespace std; struct Edge { int fm,to,cost,nxt; }E[sz2<<1]; struct edge { int fm,to,cost; }store[sz1]; int cnt=0,n,m; int fa[sz2],head[sz2]; LL cost; double ans=0; void addedge(int u,int v,int c) { E[cnt].nxt=head[u]; head[u]=cnt; E[cnt].fm=u; E[cnt].to=v; E[cnt++].cost=c; } bool cmp(edge a,edge b) { return a.cost<b.cost; } //并查集操作 int Find(int x) { return fa[x]!=x?fa[x]=Find(fa[x]):x; } void Union(int x,int y) { fa[x]=y; } //计算期望 int dfs(int x,int pre) { int res=1,tmp; for(int i=head[x];~i;i=E[i].nxt) { //不回去 if(E[i].to!=pre) { tmp=dfs(E[i].to,x); ans+=1.0*tmp*(n-tmp)*E[i].cost; res+=tmp; } } //res为该节点为根节点的子树上的节点数 return res; } int main() { int t,u,v,x,y,c,am; scanf("%d",&t); while(t--) { ans=0; cnt=am=0; cost=0; memset(head,-1,sizeof(head)); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) fa[i]=i; for(int i=0;i<m;i++) scanf("%d%d%d",&store[i].fm,&store[i].to,&store[i].cost); sort(store,store+m,cmp); //寻找最小生成树 for(int i=0;i<m;i++) { u=store[i].fm; v=store[i].to; c=store[i].cost; x=Find(u); y=Find(v); if(x!=y) { Union(x,y); am++; cost+=store[i].cost; addedge(u,v,c); addedge(v,u,c); //已经添加了n-1条边,则可以停止 if(am==n-1) break; } } dfs(1,-1); printf("%lld %.2lfn",cost,2*ans/(1LL*n*(n-1))); } return 0; }