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 hasn(n≤100000) Villages which is numbered from 1 toN. Since abandoned for a long time, the roads need to be re-built. There ism(m≤1000000) Roads to being re-built, the length of each road isWi(wi≤1000000) . Guaranteed. W is different. The roads made all the villages connected directly or indirectly before destroyed. Every road would cost the same value of it length to rebuild. The king wants to use the minimum cost to make all of the villages connected with each other directly or indirectly. After the roads is re-built, the king asks a men as messenger. The king would select any of the different points as starting point or the destination with the same probability. Now the king asks-him the minimum cost and the minimum expectations length the messenger would walk.
Inputthe first line contains an integer t(t≤) which indicates the number of test cases.
For each test case, the first line contains the integersn,m Indicate the number of villages and the number of roads to be re-built. NextmLines, each line has three numberi,J,w The length of a road connecting the village I and the village J is w .
Outputoutput the minimum cost and minimum expectations with the decimal places. They separated by a space.
Sample Input14 61 2 12 3 23 4 34 1 41 3 52 4 6
Test instructions: To find the minimum spanning tree, and then the expectation of any two-point distance.
Minimum spanning tree with kruscal. Expect to start without asking, see the puzzle find using Dfs: each side of the contribution, the contribution of an edge is equal to the edge of the | left subtree |*| right subtree |*value.
Use pair and vector in your code
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespacestd;structpath{intx, y; Doublevalue;} path[1000005];typedef pair<int,int>P;vector<P> edge[1000005];BOOLCMP (Path A,path b) {returna.value<B.value;}intfather[100005],n,m;;intFindintx) { if(x!=father[x]) father[x]=find (father[x]); returnfather[x];}voidMergeintAintb) { intx=find (a); inty=find (b); if(x!=y) father[x]=y;}Long Longsumv=0, sumd=0;voidkruscal () { for(intI=1; i<=n; i++) Father[i]=i; for(intI=0; i<=n; i++) edge[i].clear (); Sort (Path,path+m,cmp); for(intI=0; i<m; i++) { intX=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)); } }}intDfsintXintLast ) { intCnt=0; for(intI=0; I<edge[x].size (); i++) { intY=edge[x][i].first,value=Edge[x][i].second; if(last!=y) {intnow=DFS (Y,X); CNT+=Now ; SUMD+=1.0*now* (N-now) *value; } } returncnt+1;}
intMain () {intT; scanf ("%d",&t); while(t--) {SUMV=0; SUMD=0; scanf ("%d%d",&n,&m); for(intI=0; i<m; i++) {scanf ("%D%D%LF",&path[i].x,&path[i].y,&path[i].value); } kruscal (); DFS (1,-1); printf ("%i64d%.2lf\n", sumv,sumd*2.0/(1.0*N)/(n1.0)); } return 0;}
Sample OUTPUT6 3.33
Authorhit
Source
hdu_5723_ minimum Spanning tree + any two-point distance expectation