HDU 5723 Abandoned country 最小生成树+搜索
Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4477 Accepted Submission(s): 1124
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.
4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6
/*
HDU 5723 Abandoned country 最小生成树+搜索 problem:
给你n个点和m条边,让你求最少花费多少可以将所有点连通并求出任意两点的花费期望 solve:
第一个直接求最小生成树。主要是不懂它这个期望到底要求什么。看题解说的是深搜求出每条路用过
的次数来得到总花费。然后除以可能发生的次数 by——hhh
*/
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <map>
#define lson ch[r][0]
#define rson ch[r][1]
#define ll long long
#define key_val ch[ch[root][1]][0]
using namespace std;
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
int vis[maxn];
int f[maxn];
vector<pair<int,int>> q[maxn];
struct Edge
{
int u,v,w;
} edge[1000010]; int tot; void add(int u,int v,int val)
{
edge[tot].u = u,edge[tot].v = v,edge[tot++].w = val;
} bool cmp(Edge a,Edge b)
{
return a.w < b.w;
} int fin(int x)
{
if(f[x] == -1) return x;
return f[x] = fin(f[x]);
} ll cal(int n)
{
memset(f,-1,sizeof(f));
sort(edge,edge+tot,cmp);
ll cnt = 0,ans = 0;
for(int i = 0; i < tot; i++)
{
int u = edge[i].u;
int v = edge[i].v;
int w = edge[i].w;
int t1 = fin(u),t2 = fin(v);
if(t1 != t2)
{
ans = (ll)(ans + w);
f[t1] = t2;
cnt++;
q[u].push_back(make_pair(v,w));
q[v].push_back(make_pair(u,w)); }
if(cnt == n-1)
break;
}
// cout << cnt <<endl;
return ans;
}
int n;
double ans;
ll dfs(int now)
{
vis[now] = 1;
ll t = 0,ta = 0;
for(int i = 0; i < q[now].size(); i++)
{
ll v = q[now][i].first;
ll w = q[now][i].second;
if(!vis[v])
{
t = dfs(v);
ta += t;
ans = ans+1.0*t*(n-t)*w;
}
}
return ta+1;
} int main()
{
int T,a,c,b;
// freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
int m;
ll tans;
tot = 0,ans = 0;
memset(vis,0,sizeof(vis));
scanf("%d%d",&n,&m); for(int i =0; i <= n; i++)
q[i].clear();
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
if(!n || !m)
{
printf("0 0.00\n");
continue;
}
tans = cal(n);
dfs(1);
double t = (1.0*n*(n-1)/2);
// cout <<ans <<" " <<t<<endl;
printf("%I64d %.2f\n",tans,ans/t);
}
return 0;
}
HDU 5723 Abandoned country 最小生成树+搜索的更多相关文章
- hdu 5723 Abandoned country 最小生成树 期望
Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...
- hdu 5723 Abandoned country 最小生成树+子节点统计
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country (最小生成树+dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...
- 最小生成树 kruskal hdu 5723 Abandoned country
题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...
- HDU 5723 Abandoned country(落后渣国)
HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ...
- 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(2016多校第一场) (最小生成树+期望)
Abandoned country Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)
Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...
随机推荐
- C++类型萃取
stl中的迭代器和C++中的类型萃取: http://www.itnose.net/detail/6487058.html 赐教!
- recompose mapProps
mapProps介绍 mapProps函数接收一个函数参数,这个函数参数会返回一个对象用作为接下来的组件的props.组件接收到的props只能是通过mapProps函数参数返回的对象,其他的prop ...
- JAVA_SE基础——4.path的临时配置&Classpath的配置
这次,我来写下关于path的临时配置的心的 我来说个有可能的实例:如果你去到别人的电脑 又想写代码 又不想改乱别人的path配置的话 再说别人愿意你在别人的电脑上瞎配吗? 那该怎么办呢? 那没问题 ...
- LeetCode & Q122-Best Time to Buy and Sell Stock II-Easy
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- jsMath对象
Math对象: abs.用来求绝对值. ceil:用来向上取整. floor:用来向下取整. round:用来四舍五入取近似值. sqrt:用来开方. pow:括号内有2位参数.如pow(2,5)表示 ...
- Linux实战案例(7)安装jdk
一.文件准备 1.1 文件名称 jdk-8u121-linux-x64.tar.gz 1.2 下载地址 http://www.oracle.com/technetwork/java/javase/do ...
- SpringBoot 概念和起步
一.概念和由来 1.什么是 Spring Boot Spring Boot 的设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用特定方式来进行配置,从而使开发人员不再需要定义样板化 ...
- ABP CORE 框架入门视频教程《电话薄》基于 Asp.NET Core2.0 EF Core
ABP框架简介 ABP是"ASP.NET Boilerplate Project (ASP.NET样板项目)"的简称. ASP.NET Boilerplate是一个用最佳实践和流行 ...
- loadrunner录制时web时,安全证书问题
测试环境:win7+LoadRunner11+ie9 遇到的问题:用LoadRunner录制时,打开百度,总是报安全证书问题,如图所示 解决方法:Tools——Recording Options——p ...
- Python学习之再议row_input
再议raw_input birth = raw_input('birth: ') if birth < 2000: print '00前' else: print '00后' 运行结果: bir ...