【2016 Multi-University Training Contest 1】【hdu5723】Abandoned Countries

Abandoned country

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5733  5732  5731  5730  5729


多校联合第一场第一题。我读了题WZJRJ28基本秒想出正解了——先找出最小生成树再树形DP计算最小期望(其实就是期望,因为可以证明边权值唯一的情况下最小生成树唯一)——可惜他写的时候我很多细枝末节没跟他交代清楚,结果跪了15发,这第一场栽这里了。

考完了自己写了写看,果然还是太生疏了……Kruskal的并查集写错,邻接表写错,double long long各种错……调试了半天总算过了(最后还是换回了vector)  T A T 。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;

int m,n;
int wx[1000010],wy[1000010],f[100010],sum[100010];
vector<int> p[100010];
int ufs(int x){return x==f[x]?x:(f[x]=ufs(f[x]));}
long long ans;
double cost;

void dfs(int v,int pre,double key)
{
	sum[v]=1;
	for(unsigned int i=0;i<p[v].size();i++)
	{
		if(wx[p[v][i]]==pre||wy[p[v][i]]==pre)continue;
		if(wx[p[v][i]]==v)
		{
			dfs(wy[p[v][i]],v,p[v][i]);
			sum[v]+=sum[wy[p[v][i]]];
		}
		else
		{
			dfs(wx[p[v][i]],v,p[v][i]);
			sum[v]+=sum[wx[p[v][i]]];
		}
	}
	if(v!=1)
		cost+=key*(sum[v])*(n-sum[v]);
}
int main()
{
	//freopen("1001.in","r",stdin);
	int T;
	scanf("%d",&T);
	int tempa,tempb,tempw;
	while(T--)
	{
		scanf("%d%d",&n,&m);
		memset(wx,0,sizeof(wx));
		memset(wy,0,sizeof(wy));
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d%d",&tempa,&tempb,&tempw);
			wx[tempw]=tempa;
			wy[tempw]=tempb;
		}
		for(int i=1;i<=n;i++)f[i]=i;
		for(int i=1;i<=n;i++)p[i].clear();
		ans=0;
		for(int i=1;i<=1000000;i++)
		{
			if(!wx[i])continue;
			if(ufs(wx[i])==ufs(wy[i]))continue;
			f[f[wy[i]]]=f[wx[i]];
			ans+=((long long)i);
			p[wx[i]].push_back(i);
			p[wy[i]].push_back(i);
		}
		cost=0;
		dfs(1,0,0);
		double temp=((long long)(n-1)*n/2);
		printf("%I64d ",ans);
		printf("%.2lf\n",cost/temp);
	}
	
	
	return 0;
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值