The title says that the weights of each edge are different, indicating that the minimum spanning tree is unique and there is no minimum expectation.
Then it is to find the minimum spanning tree, arbitrarily determine a root node, calculate how many nodes of each point subtree, recorded as C[x].
This edge of the x is counted as c[x]* (N-c[x]). Then basically you can figure out the answer.
#include <cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<Set>#include<queue>#include<stack>#include<iostream>using namespaceStd;typedefLong LongLL;Const intMAXN =100000+Ten;intT, N, M, F[MAXN], C[MAXN];BOOLFLAG[MAXN];structEdge {intU, v; LL cost; }e[Ten*MAXN], EE[MAXN]; Vector<int>G[MAXN];DoubleFZ, FM;intFind (intx) {if(x = f[x]) f[x] = Find (f[x]);returnf[x];}BOOLcmpConstEdge&a,ConstEDGE&B) {returnA.cost <B.cost;}voidDfsintXintf) {Flag[x]=1; C[X] =1; for(inti =0; I < g[x].size (); i++) { intid = g[x][i];if(FLAG[EE[ID].V])Continue; DFS (EE[ID].V,ID); C[X]= C[x] +C[EE[ID].V]; } if(F! =-1) FZ= FZ +1.0*ee[f].cost*1.0*C[X] *1.0* (N-c[x]);}intMain () {scanf ("%d", &T); while(t--) {scanf ("%d%d", &n, &m); for(inti =1; I <= m; i++) scanf ("%d%d%lld", &e[i].u, &E[I].V, &e[i].cost); Sort (e+1, E +1+m, CMP); LL ans=0; for(inti =1; I <= N; i++) F[i] =I, g[i].clear (); intSZ =0; for(inti =1; I <= m; i++) { intFX = Find (e[i].u), FY =Find (E[I].V); if(FX! =FY) {F[FX]= fy, ans = ans +E[i].cost; EE[SZ].U= e[i].u, ee[sz].v = e[i].v, Ee[sz].cost =E[i].cost; G[e[i].u].push_back (SZ++); EE[SZ].U= e[i].v, ee[sz].v = e[i].u, Ee[sz].cost =E[i].cost; G[e[i].v].push_back (SZ++); }} memset (c, FZ=0,sizeofc); MEMSET (flag, FM=0,sizeofflag); DFS (1, -1); FM=1.0*n*1.0* (N-1) /2.0; printf ("%lld%.2lf\n", ans, FZ/FM); } return 0;}
HDU 5723 Abandoned country