hdu 5723 Abandoned country

本文介绍了一种算法,该算法通过构建最小生成树来解决一个特定问题:在一个重建道路网络的问题中找到连接所有村庄的最低成本方案,并计算信使在这些村庄间行走的期望最短路径长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



一开始想生成最小生成树后树DP的,后来发现只要统计每条边对答案的贡献就好了。

每条边的贡献是两边节点数相乘

最后用这个值乘2再除以n(n+1)就可以得到结果

#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000") 
using namespace std;
struct line
{
	int s,t;
	double x;
	int next;
	bool operator <(line y) const
	{
		return x<y.x;
	}
}a[200001],x[1000001];
int head[100001];
int edge;
inline void add(int s,int t,double x)
{
	a[edge].next=head[s];
	head[s]=edge;
	a[edge].s=s;
	a[edge].t=t;
	a[edge].x=x;
}
int fa[100001];
double son[100001];
inline int find(int x)
{
	if(x!=fa[x])
		fa[x]=find(fa[x]);
	return fa[x];
}
inline void dfs(int d)
{
	son[d]=0;
	int i;
	for(i=head[d];i!=0;i=a[i].next)
	{
		int t=a[i].t;
		if(fa[d]!=t)
		{
			fa[t]=d;
			dfs(t);
			son[d]+=son[t]+1;
		}
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T>0)
	{
		T--;
		int n,m;
		scanf("%d%d",&n,&m);
		int i;
		for(i=1;i<=m;i++)
			scanf("%d%d%lf",&x[i].s,&x[i].t,&x[i].x);
		sort(x+1,x+1+m);
		edge=0;
		memset(head,0,sizeof(head));
		for(i=1;i<=n;i++)
			fa[i]=i;
		double sx=0;
		int ss=0;
		for(i=1;i<=m;i++)
		{
			int fx=find(x[i].s),fy=find(x[i].t);
			if(fx!=fy)
			{
				ss++;
				fa[fx]=fy;
				edge++;
				add(x[i].s,x[i].t,x[i].x);
				edge++;
				add(x[i].t,x[i].s,x[i].x);
				sx+=x[i].x;
				if(ss==n-1)
					break;
			}
		}
		memset(fa,0,sizeof(fa));
		dfs(1);
		double sum=0;
		for(i=1;i<=edge;i++)
		{
			int s=a[i].s,t=a[i].t;
			if(fa[s]==t)
				sum+=a[i].x*(double)(son[1]-son[s])*(double)(son[s]+1);
		}
		printf("%.0f %.2f\n",sx,sum*2.0/(double)(n)/(double)(n-1));
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值