hdu5723 Abandoned country(最小生成树+任意点对距离)

思路:先跑一个最小生成树然后建好生成树,DFS直接求出任意点对距离求平均即可

坑点:各种爆int


#include<bits/stdc++.h>
using namespace std;
#define LL long long
int cas=1,T;
const int maxn = 100000+500;
const int maxm = 1000000+500;
struct Edge
{
	int u,v;
	LL dist;
	Edge(){}
	Edge(int u,int v,LL d):u(u),v(v),dist(d){}
	bool operator < (const Edge&rhs)const
	{
		return dist < rhs.dist;
	}
};

int n,m;
Edge edges[maxm];
int pre[maxn];
int Find(int x)
{
	return pre[x]==-1?x:pre[x]=Find(pre[x]);
}
void init()
{
	m=0;
	memset(pre,-1,sizeof(pre));
}
void AddEdge(int u,int v,LL dist)
{
	edges[m++]=Edge(u,v,dist);
}
struct Node
{
	int v;
	LL len;
	Node(){};
	Node(int vv,LL lenn):v(vv),len(lenn){};
};
vector<Node>vet[maxn];
double dp[maxn];
int siz[maxn];
void dfs(int root,int fa)
{
    siz[root]=1;
    for(int i = 0;i<vet[root].size();i++)
	{
		int son = vet[root][i].v;
		LL len = vet[root][i].len;
		if(son==fa)
			continue;
		dfs(son,root);
		siz[root]+=siz[son];
        dp[root]+=dp[son]+((LL)siz[son]*(LL)(n-siz[son]))*(double)len;
	}
}

LL Kruskal()
{
	LL sum = 0;
	int cnt = 0;
	sort(edges,edges+m);
	for (int i = 0;i<m;i++)
	{
		int u = edges[i].u;
		int v = edges[i].v;
		if (Find(u) != Find(v))
		{
			pre[Find(u)] = Find(v);
			sum+=edges[i].dist;
			vet[u].push_back(Node(v,edges[i].dist));
			vet[v].push_back(Node(u,edges[i].dist));
			if (++cnt >= n-1)
				break;
		}
	}
	return sum;
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{	
		int flag = 0;
		int mm;
		scanf("%d%d",&n,&mm);
		if(mm==0)
			flag =1;
		init();
		for (int i = 0;i<mm;i++)
		{
		  int a,b;
		  LL c;
		  scanf("%d%d%I64d",&a,&b,&c);
          AddEdge(a,b,c);
		}
		LL ans = Kruskal();
		printf("%I64d ",ans);
		memset(siz,0,sizeof(siz));
		memset(dp,0,sizeof(dp));
		dfs(1,-1);
		LL ress = (LL)n*(LL)(n-1);
		LL res = ress/2;
		if(!flag)
		   printf("%.2lf\n",(double)dp[1]/res);
		else
			printf("0.00\n");
		for(int i = 0;i<=n;i++)
			vet[i].clear();
	}
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}




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



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值