Abandoned country(hdu5723)

Abandoned country

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


Problem Description
An abandoned country has  n(n100000)  villages which are numbered from 1 to  n . Since abandoned for a long time, the roads need to be re-built. There are  m(m1000000)  roads to be re-built, the length of each road is  wi(wi1000000) . 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(T10)  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


首先注意到任意两条边的边权是不一样的,由此得知最小生成树是唯一的,最小生成树既然 是唯一的,那么期望其实也就是唯一的,不存在什么最小期望。求完最小生成树之后,接下 来的问题就可以转换成在最小生成树上求任意两点之间距离的平均值,对于每条边,统计所 有的路径用到此边的次数,也就是边的两端的点数之积。那么这条边的总贡献就是次数*边 权。最后得到所有边的贡献之和再除以总路径数n*(n-1)/2n(n1)/2就是答案。可以OnOn求出。任取一点为根dfs,对每个点ii记录其子树包含的点数(包括其自身),设点数为sum[i]sum[i],则ii的父亲一侧的点数即为n-sum[i]nsum[i]。一边遍历一边统计就行。

#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;
const int SIZE=1e5+10;
const int maxn=1<<30;
struct Edge{
	int u,v,w;
	bool operator<(const Edge &other )const{
		return w<other.w;
	}
}e[1001000];
struct ge{
	int v,w,next;
	ge(){}
	ge(int x,int y,int z):v(x),w(y),next(z){}
}edge[2*SIZE];
int head[SIZE],tot,parent[SIZE],n,m,numP[SIZE];
LL sum[SIZE];
void init(){
	tot=0;
	memset(head,-1,sizeof(head));
	memset(sum,0,sizeof(sum));
	memset(numP,0,sizeof(numP));
	for(int i=1;i<SIZE;i++)
		parent[i]=i;
}
void addEdge(int u,int v,int w){
	edge[tot].v=v;
	edge[tot].w=w;
	edge[tot].next=head[u];
	head[u]=tot++;
}
int getroot(int x){
	if(x==parent[x])return x;
	return parent[x]=getroot(parent[x]);
}
void Merge(int x,int y){
	int fx=getroot(x);
	int fy=getroot(y);
	if(fx==fy)return;
	parent[fx]=fy;
}
LL MST(int n,int m){
	int cnt=0;
	LL ret=0;
	for(int i=0;i<m;i++){
		int u=e[i].u;
		int v=e[i].v;
		if(getroot(u)==getroot(v))continue;
		Merge(u,v);
		addEdge(u,v,e[i].w);
		addEdge(v,u,e[i].w);
		cnt++;
		ret+=e[i].w;
		if(cnt==n-1)break;
	}
	return ret;	
}
void dfs(int u,int pre){
	for(int i=head[u];~i;i=edge[i].next){
		int v=edge[i].v;
		if(v==pre)continue;
		dfs(v,u);
		sum[u]+=(LL)(numP[v])*(n-numP[v])*edge[i].w;
		numP[u]+=numP[v];
	}
	numP[u]++;
}
int main(){
	int T;
	scanf("%d",&T);
	for(int cas=1;cas<=T;cas++){
		init();
		scanf("%d%d",&n,&m);
		for(int i=0;i<m;i++){
			scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
		}
		sort(e,e+m);
		LL ans=MST(n,m);
		dfs(1,-1);
		LL cnt=0;
		for(int i=1;i<=n;i++){
			cnt+=sum[i];
		}
		printf("%lld %.2lf\n",ans,double(cnt)/n/(n-1)*2);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值