任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723

Abandoned country

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

Problem Description
An abandoned country has n(n≤100000) 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.
 
Input
The first line contains an integer T(T≤10) 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

题意概括:

有一个 N 个点 M 条边的无向图,题目需要求最小生成树路径和, 以及在这颗树上任意两点的期望(任意两点概率都一样,两点的期望即: 两点路径的权值 / C(N,2)  )

解题思路:

求最小生成树:选择堆优化的Prim 或者 kruskal (都是 O(N log M)。

如果分开单独考虑树上两点的期望值 很容易想到最短路,不过应该超时。

总体来看,我们要求的是期望和,所以只要直到经过每条路径的次数,总路程就等于求和每一条路径花费乘以出现次数,方法是 dfs。

最后算出 总的路程 / C(N, 2) 就可以了。

tip:注意数据类型要用到 long long,期望定义为double.

AC code:

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e5+;
const int MAXM = 1e6+;
int ff[MAXN];
bool vis[MAXN];
vector<pair<int, int> > ttp[MAXN];
int N, M;
int T_case;
LL ans2;
LL ans;
struct edge
{
int u, v, cost;
}e[MAXM]; bool cmp(const edge& e1, const edge& e2)
{
return e1.cost < e2.cost;
} void init()
{
memset(e, ,sizeof(e));
memset(vis, false, sizeof(vis));
for(int i = ; i <= N; i++){ff[i] = i;}
for(int i = ; i <= N; i++) ttp[i].clear();
ans = ;
ans2 = ;
} int ufind(int u)
{
if(u == ff[u]) return ff[u];
ff[u] = ufind(ff[u]);
return ff[u];
} void unite(int u, int v)
{
u = ufind(u);
v = ufind(v);
if(u == v) return;
ff[u] = v;
} void kruskal()
{
ans = ;
sort(e, e+M, cmp);
for(int i = ; i < M; i++){
edge es = e[i];
if(ufind(es.u) != ufind(es.v)){
unite(es.u, es.v);
ans += es.cost;
ttp[es.u].push_back(make_pair(es.v, es.cost));
ttp[es.v].push_back(make_pair(es.u, es.cost));
}
}
} LL dfs(int x)
{
vis[x] = true;
LL now = , all = ;
for(int i = ; i < ttp[x].size(); i++){
int b = ttp[x][i].first;
if(!vis[b]){
now = dfs(b);
all+=now;
ans2+=now*(N-now)*ttp[x][i].second;
}
}
return all;
} int main()
{
scanf("%d", &T_case);
while(T_case--){
scanf("%d%d", &N, &M);
init();
for(int i = ; i < M; i++){
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].cost);
}
kruskal();
dfs();
double zz = 1.0*N*(N-)/;
//printf("%.2lf %lld\n", zz, ans2);
printf("%lld %.2lf\n", ans, (double)ans2/zz);
//double kk = 1.00/6+2.00/6+3.00/6+3.00/6+4.00/6+5.00/6;
//printf("%.2lf\n", kk);
}
return ;
}

HDU 5723 Abandoned country 【最小生成树&&树上两点期望】的更多相关文章

  1. HDU 5723 Abandoned country 最小生成树+搜索

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  2. hdu 5723 Abandoned country 最小生成树 期望

    Abandoned country 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...

  3. hdu 5723 Abandoned country 最小生成树+子节点统计

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. HDU 5723 Abandoned country (最小生成树+dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 n个村庄m条双向路,从中要选一些路重建使得村庄直接或间接相连且花费最少,这个问题就是很明显的求最 ...

  5. 最小生成树 kruskal hdu 5723 Abandoned country

    题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...

  6. HDU 5723 Abandoned country(落后渣国)

    HDU 5723 Abandoned country(落后渣国) Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 ...

  7. HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)

    Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...

  8. hdu 5723 Abandoned country(2016多校第一场) (最小生成树+期望)

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  9. HDU 5723 Abandoned country (最小生成树 + dfs)

    Abandoned country 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5723 Description An abandoned coun ...

随机推荐

  1. pandas中,dataframe 进行数据合并-pd.concat()

    ``# 通过数据框列向(左右)合并 a = pd.DataFrame(X_train) b = pd.DataFrame(y_train) # 合并数据框(合并前需要将数据设置成DataFrame格式 ...

  2. Oracle 数据库和Sql Server数据库的区别

    Oracle数据库的访问方式,和SqlServer数据库是有很大差别的,下面用图来说明: 1.Sql Server数据库 SqlServer数据库的访问方式,大致是:假设用户通过sa登录SqlServ ...

  3. Zookeeper概念学习系列之zookeeper是什么?

    1. Zookeeper是Hadoop的分布式协调服务. 2. 分布式应用程序可以基于它,来实现同步服务,配置维护和命名服务等. 3. zookeeper可以保证数据在zookeeper集群之间的数据 ...

  4. 05-ognl基本语法

    1 基本取值 @Test //1基础语法演示-基本取值 //取出root中的属性值 public void fun2() throws Exception{ //1 准备OGNLcontext Ogn ...

  5. django管理界面使用与bootstrap模板使用

    一.bootstrap模板使用 1.去bootstrap官网找一个合适的模板,下载下来,右键另存为即可 bootstrap官网---->bootstrap中文文档3-------->起步- ...

  6. 深入理解JavaScript系列(22):S.O.L.I.D五大原则之依赖倒置原则DIP

    前言 本章我们要讲解的是S.O.L.I.D五大原则JavaScript语言实现的第5篇,依赖倒置原则LSP(The Dependency Inversion Principle ). 英文原文:htt ...

  7. ionic2 目录

    首先 ionic2 暂时找不到中文文档.本人英语又很渣.无奈之下只能依赖于百度翻译.完全是已自己理解的方式运作 ,可能里面会有一些偏差之类的 不过我都测试过代码是可以跑通的 只不过讲解的部分可能... ...

  8. SpringMVC框架下实现分页功能

    1.创建实体类Page.java @Entity public class Page { private int totalRecord;// 表示查询后一共得到多少条结果记录 private int ...

  9. servlet中this.getServletContext(); this.getServletConfig().getServletContext(); 的区别

    WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用.ServletConfig对象中维护了ServletContext对象的引用,开发人 ...

  10. Oracle JDBC 连接卡死后 Connection Reset

    坑 这绝对是我碰计算机以来遇到的第一大坑! 症状: 在Linux主机上远程登录,执行一个简单的Oracle的JDBC连接程序(jar包),结果硬生生的卡在了连接建立验证阶段,然后等上几分钟后因为连接超 ...