【HDU5723 2016 Multi-University Training Contest 1A】【最小生成树 + 简单算贡献】 Abandoned country 最小生成树的链长之和

Abandoned country

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


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


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e6+10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
int n, m;
struct A
{
	int x, y, z;
	bool operator < (const A&b)const
	{
		return z < b.z;
	}
}a[N];
vector< pair<int,int> >b[N];
int f[N];
int find(int x)
{
	return f[x] == x ? f[x] : f[x] = find(f[x]);
}
int son[N];
LL ans1;
double ans2;
void dfs(int x, int fa)
{
	son[x] = 1;
	for (int i = b[x].size() - 1; ~i; --i)
	{
		int y = b[x][i].first;
		if (y == fa)continue;
		dfs(y,x);
		son[x] += son[y];
		ans2 += (double)son[y] * (n - son[y])*b[x][i].second;
	}
}
int main()
{
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d%d", &n, &m);
		double K = (LL)n*(n - 1) / 2;
		for (int i = 1; i <= n; ++i)
		{
			f[i] = i;
			b[i].clear();
		}
		for (int i = 1; i <= m; ++i)
		{
			scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z);
		} sort(a + 1, a + m + 1);
		ans1 = ans2 = 0;
		for (int i = 1; i <= m; ++i)
		{
			int x = a[i].x;
			int y = a[i].y;
			int z = a[i].z;
			int fx = find(x);
			int fy = find(y);
			if (fx != fy)
			{
				f[fy] = fx;
				ans1 += z;
				b[x].push_back(MP(y, z));
				b[y].push_back(MP(x, z));
			}
		}
		dfs(1, 0);
		printf("%lld %.2f\n", ans1, ans2 / K);
	}
	return 0;
}
/*
【trick&&吐槽】
我是sb,明明小心过爆int,结果还是在n*n(-1)的乘法爆int了>.<

【题意】
n点m边(每边权值都不同)
在求出最小生成树之后,让你算出对于C(n,2)条链的长度之和

【类型】
最小生成树+贡献计算

【分析】
如果每边权值都不同,那么最小生成树是否唯一?
答案是唯一的。
在这个基础上,我们直接求最小生成树,并算贡献即可

【时间复杂度&&优化】
O(mlogm)

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值