同:http://www.cnblogs.com/xiao-xin/articles/4196009.html
顺手也给做了,更简单了一些==
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 #define LL long long 6 LL now,head[200005],next[200005],point[200005]; 7 LL down[200005],l[200005],r[200005],w[200005]; 8 void add(LL u,LL v) 9 { 10 next[++now]=head[u]; 11 head[u]=now; 12 point[now]=v; 13 } 14 void dfs(LL pre,LL u) 15 { 16 down[u]=1; 17 for (LL i=head[u];i!=-1;i=next[i]) 18 { 19 LL v=point[i]; 20 if (v==pre) continue; 21 dfs(u,v); 22 down[u]+=down[v]; 23 } 24 } 25 int main() 26 { 27 LL T,i,n,tmp,sum; 28 double ans; 29 scanf("%I64d",&T); 30 while (T--) 31 { 32 memset(head,-1,sizeof(head)); 33 now=0; 34 scanf("%I64d",&n); 35 for (i=1;i<n;i++) 36 { 37 scanf("%I64d%I64d%I64d",&l[i],&r[i],&w[i]); 38 add(l[i],r[i]); 39 add(r[i],l[i]); 40 } 41 sum=n*(n-1)/2; 42 dfs(-1,0); 43 ans=0.0; 44 for (i=1;i<n;i++) 45 { 46 tmp=min(down[l[i]],down[r[i]]); 47 ans=ans+1.0*w[i]*tmp*(n-tmp)/sum; 48 } 49 printf("%lf\n",ans); 50 } 51 }