HDU5723 Abandoned country(最小生成树,数学期望)

Abandoned country

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


Problem Description
An abandoned country has $n (n \leq 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 \leq 1000000)$ roads to be re-built, the length of each road is $w_{i} (w_{i} \leq 1000000)$. Guaranteed that any two $w_{i}$ 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 (T \leq 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, w_{i}$, the length of a road connecting the village $i$ and the village $j$ is $w_{i}$.
 

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

题意:生成一个最小生成树,然后算随意取两点的距离的期望值。

思路:生成树简单,期望值任意两点的距离加起来除以取点的方案数,每次取点都有要经过的边,所以枚举每条边看看其被多少对顶点经过就可以了。

经过的次数就是这条边两边点的个数之积。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath> 
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
const int ff = 0x3f3f3f3f;
struct node
{
	int x;
	int y;
	int dis;
} edge[maxn];

int n,m;
int pre[maxn];
int num[maxn];
vector<int> mp[maxn];

bool cmp(node a,node b)
{
	return a.dis< b.dis;
}

int find(int x)
{
	return pre[x] == x?x:pre[x] = find(pre[x]);
}

int dfs(int x,int p)
{
	int sum = 1,k = mp[x].size();
	for(int i = 0;i< k;i++)
	{
		if(mp[x][i] == p)
			continue;
		sum+= dfs(mp[x][i],x);
	}
	return num[x] = sum;//记录子节点个数
}

void init()
{
	mem(num,0);
	mem(edge,0);
	for(int i = 1;i<= n;i++)
		pre[i] = i,mp[i].clear();
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		scanf("%d %d",&n,&m);
		init();
		int cnt = 0,k = 0;
		for(int i = 1;i<= m;i++)
		{
			cnt++;
			scanf("%d %d %d",&edge[cnt].x,&edge[cnt].y,&edge[cnt].dis);
		}
		
		sort(edge+1,edge+m+1,cmp);
		double ans1 = 0,ans2 = 0;
		for(int i = 1;i<= cnt;i++)
		{
			int fx = find(edge[i].x);
			int fy = find(edge[i].y);
			if(fx!= fy)
			{
				pre[fx] = fy;
				edge[++k] = edge[i];
				mp[edge[i].x].push_back(edge[i].y);
				mp[edge[i].y].push_back(edge[i].x);
				ans1+= edge[i].dis;
			}
		}
		
		num[edge[1].x] = dfs(edge[1].x,-1);
		for(int i = 1;i<= k;i++)
		{
			int tmp = min(num[edge[i].x],num[edge[i].y]);
			ans2+= (double)edge[i].dis*(n-tmp)*tmp;
		}
		ans2/= (double)(n-1)*n/2;
		printf("%.0f %.2f\n",ans1,ans2);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值