題目解析:最小生成樹+樹形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
?
題意:
???? 給定一張圖,求最小生成樹,并求在圖中任取兩點(diǎn),兩點(diǎn)間路徑代價(jià)的期望值。
解題:
??? 因?yàn)榍舐窂酱鷥r(jià)都是唯一的,求兩點(diǎn)間路徑代價(jià)最小值,即求最小生成樹上的路徑最小值。代價(jià)是路徑邊上的值,故我們可以考慮最小生成樹上的邊,被取到的概率乘以其權(quán)值,累加邊代價(jià)期望,即可得到總期望。而每條邊被取到的概率為該邊兩側(cè)的點(diǎn)數(shù)量的乘積除以C(n,2)。
???? 先求最小生成樹,并在尋找樹的過程中,保留最小生成樹上的邊,用于后續(xù)計(jì)算期望。采用dfs的方式,任意從樹上一點(diǎn)出發(fā),計(jì)算該節(jié)點(diǎn)所在的子樹上的節(jié)點(diǎn)數(shù)x,并由總數(shù)減去x得到邊另一側(cè)的節(jié)點(diǎn)數(shù)。
代碼:
#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; } //計(jì)算期望 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為該節(jié)點(diǎn)為根節(jié)點(diǎn)的子樹上的節(jié)點(diǎn)數(shù) 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); //已經(jīng)添加了n-1條邊,則可以停止 if(am==n-1) break; } } dfs(1,-1); printf("%lld?%.2lfn",cost,2*ans/(1LL*n*(n-1))); } return?0; }