HDU 5723(最小生成树+dfs)

Abandoned country

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


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


题目大意:

给n个点m条边,求出最小生成树路径长度,和任意两点之间的距离的期望(所有路径长度的总和除以n*(n-1)/ 2)。

思路:

因为图像有100000个点,prim会超时,所以用 kruskal算法求最小生成树。

接下来求所有路径总长度。

因为这是最小生成树图像,所以每条边在总路径长度中出现的次数就是去掉这条路后两边的连接点的乘积,用dfs找出一边的连接点num,另一边就是n-num,路径总长度就是dis*num*(n-num)。


#include<iostream> 
#include<stdio.h> 
#include<math.h> 
#include<string.h> 
#include<vector> 
#include<queue> 
#include<map> 
#include<stack> 
#include<queue> 
#include<algorithm> 
using namespace std; 
long long pre[100005],n,m;
bool vis[100005];
double sum;
vector<pair<int,int>> v[100005];
struct edge
{
	long long from,to,dis;
};
edge e[1000005];
bool cmp(edge a,edge b)
{
	if(a.dis<b.dis)return true;
	return false;
}
long long find(long long x) //查找根结点+路径压缩  
{  
    if(x==pre[x])return x;
	pre[x]=find(pre[x]);
	return pre[x];
}
bool fix(int x,int y)
{
	long long fx=find(x),fy=find(y);
	if(fx==fy)return false;
	else
	{
		pre[fy]=fx;
		return true;
	}
}

long long kru()
{
	int cnt=1;
	long long dis=0;
	for(int i=0;i<m;i++)
	{
		if(fix(e[i].from,e[i].to))
		{
			cnt++;
			dis+=e[i].dis;
			v[e[i].from].push_back(make_pair(e[i].to,e[i].dis));
			v[e[i].to].push_back(make_pair(e[i].from,e[i].dis));
		}
		if(cnt==n)
			break;
	}
	return dis;
}
long long dfs(int x)
{
	vis[x]=1;
	long long sumson=0;
	for(int i=0;i<v[x].size();i++)
	{
		if(!vis[v[x][i].first])
		{
			long long son=dfs(v[x][i].first)+1;
			sum+=v[x][i].second*son*(n-son);
			sumson+=son;
		}
	}
	vis[x]=0;
	return sumson;
}
int main()
{
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%I64d%I64d",&n,&m);
		sum=0;
		for(int i=1;i<=n;i++)
		{
			pre[i]=i;
			v[i].clear();
			vis[i]=0;
		}
		for(int i=0;i<m;i++)
			scanf("%I64d%I64d%I64d",&e[i].from,&e[i].to,&e[i].dis);
		sort(e,e+m,cmp);
		long long dd=kru();
		dfs(1);
		printf("%I64d %.2f\n",dd,sum/(n*(n-1)/2));
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值