第一问就是裸MST。。

第二问求2点之间距离期望。。就是把距离和全求一遍然后除以n(n-1)嘛。。

所以就是求距离和了。。经典树DP。。

对每个点X分别枚举累加节点深度和(深度指到子树根的距离),对每个儿子t,只要用距离和乘t的节点数,再加上t距离和乘以当前x的节点数,就能算出经过x的路径和了。。


#include<bits/stdc++.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define inf 1e9
#define ll long long
#define link(x) for(edge*j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define NM 100005
#define nm 1000005
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return x*f;
}

struct edge{ll t,v;edge*next;}e[2*nm],*h[NM],*o=e;
void add(int x,int y,ll v){o->t=y;o->v=v;o->next=h[x];h[x]=o++;}
struct tmp{
int a,b,c;
bool operator<(const tmp&o)const{return c<o.c;}
}a[nm];
int n,m,f[NM],size[NM];
ll s;
double ans,mod,d[NM];
int fin(int x){return f[x]==x?x:f[x]=fin(f[x]);}
void dfs(int x){
size[x]=1;d[x]=0;
link(x)if(!f[j->t]){
f[j->t]=x;
dfs(j->t);d[j->t]+=j->v*(ll)size[j->t]/mod;
ans+=(ll)size[x]*d[j->t]+(ll)size[j->t]*d[x];
d[x]+=d[j->t];size[x]+=size[j->t];
}
}

int main(){
int _=read();
while(_--){
mem(f);mem(e);mem(h);o=e;mem(d);mem(size);ans=s=0LL;
n=read();m=read();mod=n*1LL*(n-1)/2;
inc(i,1,m){
a[i].a=read();a[i].b=read();a[i].c=read();
}
inc(i,1,n)f[i]=i;
sort(a+1,a+1+m);
inc(i,1,m){
int x=fin(a[i].a),y=fin(a[i].b);
if(x==y)continue;
f[x]=y;s+=a[i].c;add(a[i].a,a[i].b,a[i].c);add(a[i].b,a[i].a,a[i].c);
}
inc(i,1,n)f[i]=0;
printf("%lld ",s);
dfs(f[1]=1);
printf("%.2f\n",ans);
}
return 0;
}





Abandoned country

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6957    Accepted Submission(s): 1703


Problem Description


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

 


Input


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 ​


 


Recommend


wange2014   |   We have carefully selected several similar problems for you:   ​​6275​​​  ​​​6274​​​  ​​​6273​​​  ​​​6272​​​  ​​​6271​​ 

 


​Statistic​​ | 

​Submit​​ | 

​Discuss​​ |

​Note​