hdu 5723 Abandoned country(2016多校第一场) (最小生成树+期望)
Abandoned country
Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1756    Accepted Submission(s):
475
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≤1000000)
roads to be re-built, the length of each road is wi(wi≤1000000).
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.
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.
two decimal places. They separated by a space.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define M 200010
#define ll long long
using namespace std;
ll n,m; struct edge
{
int u,v;
int w;
} e[M*]; struct node
{
int u,v,w;
int ans; ///ans记录经过这条边能达到的边的个数
int next; ///next记录上一次连接的边的序号
}ee[M]; int father[M],head[M]; ///father数组表示根节点,head记录每条边最后一次连接的边的序号
ll ans,sum;
int tol; ll dfs(int u,int p) ///搜索
{
ll num=;
for(int i=head[u];i!=-;i=ee[i].next)
{
if(ee[i].v!=p)
{
ee[i].ans+=dfs(ee[i].v,ee[i].u);
sum+=ee[i].ans*(n-ee[i].ans)*ee[i].w; ///此边能到达的边的个数*能到此边的边的个数*边的长度
// cout<<ee[i].ans<<" "<<n-ee[i].ans<<" "<<ee[i].w<<endl;
//cout<<sum<<endl;
num+=ee[i].ans;
}
}
return num;
} void add_node(edge a) ///建立邻接表
{
ee[tol].u=a.u; ///正向
ee[tol].v=a.v;
ee[tol].w=a.w;
ee[tol].ans=;
ee[tol].next=head[a.u];
head[a.u]=tol++;
ee[tol].u=a.v; ///反向
ee[tol].v=a.u;
ee[tol].w=a.w;
ee[tol].ans=;
ee[tol].next=head[a.v];
head[a.v]=tol++;
} int find(int x) ///搜索根节点
{
while(x!=father[x])
x=father[x];
return x;
} void sourch(int x,int y,int z,edge ss)
{
x=find(x);
y=find(y);
if(x!=y) ///若根节点不相同
{
add_node(ss); ///并且此边存在,建立邻接表
father[x]=y; ///将其连接在一起
ans+=z;
} } void init()
{
ans=,sum=;
int i,j;
for(i=; i<=n; i++)
{
father[i]=i; ///将所有父节点初始定义为自己本身
head[i]=-;
}
tol=;
for(i=; i<m; i++)
{
sourch(e[i].u,e[i].v,e[i].w,e[i]); ///求最小生成树
}
dfs(,-);
ll nn=n*(n-)/;
printf("%I64d %.2lf\n",ans,(double)sum/nn);
} bool cmp(edge a,edge b)
{
return a.w<b.w;
} int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&m);
for(i=; i<m; i++)
{
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); ///记录每边的数据
}
sort(e,e+m,cmp); ///要选取最小的花费,因此要进行排序
init();
}
return ;
}
hdu 5723 Abandoned country(2016多校第一场) (最小生成树+期望)的更多相关文章
- HDU 5723 Abandoned country(落后渣国)
		HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ... 
- HDU 5723 Abandoned country 最小生成树+搜索
		Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ... 
- 最小生成树 kruskal hdu 5723 Abandoned country
		题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ... 
- HDU 5723 Abandoned country 【最小生成树&&树上两点期望】
		任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ... 
- HDU 5723  Abandoned country (最小生成树 + dfs)
		Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ... 
- hdu 5723 Abandoned country 最小生成树 期望
		Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ... 
- HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)
		Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ... 
- hdu  5723    Abandoned country   最小生成树+子节点统计
		Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ... 
- HDU 5723 Abandoned country(最小生成树+边两边点数)
		http://acm.split.hdu.edu.cn/showproblem.php?pid=5723 题意:给出一个无向图,每条路都有一个代价,求出把所有城市连通的最小代价.在此基础上,国王会从这 ... 
随机推荐
- hystrix熔断器
			在一个具有多服务的应用中,假如由于其中某一个服务出现问题,导致响应速度变慢,或是根本没有响应返回,会导致它的服务消费者由于长时间的等待,消耗尽线程,进而影响到对其他服务的线程调用,进而会转变为整个应用 ... 
- 洛谷P2982 [USACO10FEB]慢下来Slowing down  [2017年四月计划 树状数组01]
			P2982 [USACO10FEB]慢下来Slowing down 题目描述 Every day each of Farmer John's N (1 <= N <= 100,000) c ... 
- 【python之路19】文件操作
			一.打开文件 文件句柄 = open('文件路径', '模式') 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 打开文件的模式有: r ... 
- 模拟退火解TSP问题MATLAB代码
			分别把前四个函数存成m文件,再运行最后一个. swap.m function [ newpath , position ] = swap( oldpath , number ) % 对 oldpath ... 
- D2D画箭头的例子
			原文:D2D画箭头的例子 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sunnyloves/article/details/50830102 用处 ... 
- ubuntu上安装notepadpp
			Notepad++是一套非常有特色的自由软件的纯文字编辑器(许可证:GPL).有完整的中文化接口及支持多国语言编写的功能(UTF8 技术).它的功能比 Windows 中的 Notepad(记事本)强 ... 
- CMake学习笔记三
			添加子文件夹 ADD_SUBDIRECTORY(hello) ADD_SUBDIRECTORY(libs) ADD_SUBDIRECTORY(VSLIB) SET(IDE_TARGET_NAME VS ... 
- epiinfo是美国CDC开发维护的流行病学数据录入和分析软件,在DOS时代占主流,随着Windows的普及用的人越来越少了,epiinfo重新开发Windows版本后,体积庞大且不好用。在数据录入方面已被EpiData取代,不过epiinfo的分析模块,比如地理信息系统某些情况下还是挺有用的。
			Manual: https://www.cdc.gov/epiinfo/support/userguide.html https://en.wikipedia.org/wiki/Epi_Info 教程 ... 
- 模板内置函数(HTML)
			模板内置函数 注意:1.html书写避免多余的空格,否则可能无法被识别 2.模板是用来渲染的不要用来处理逻辑 后台ctime=datetime.datatime.now() {{ctime|date: ... 
- 通过IP地址訪问Jbossserver上的应用
			版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/liu765023051/article/details/28882533 环境介绍 Web项目中.在 ... 
