任意门: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. mysql大数据表删除操作锁表,导致其他线程等待锁超时(Lock wait timeout exceeded; try restarting transaction;)

    背景: 1.有一个定时任务,每10分钟入一批统计数据: 2.另一个定时任务,每天定时清理7天前数据,此定时任务每天01:18:00执行: 现象: 每天01:20:00的统计数据入库失败,异常信息如下, ...

  2. CentOS 6.7 安装配置 nagios-server

    作者博文地址:https://www.cnblogs.com/liu-shuai/ 一.简介    Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态, ...

  3. unity读取json文件

    首先填表 [escel转json]注意,粘贴表之后,需要把最后的空行删掉 http://www.bejson.com/json/col2json/ [json格式化] http://www.bejso ...

  4. socket 客户端和服务端通信

    客户端要连接服务器:首先要知道服务器的IP地址.而服务器里有很多的应用程序,每一个应用程序对应一个端口号 所以客户端想要与服务器中的某个应用程序进行通信就必须要知道那个应用程序的所在服务器的IP地址, ...

  5. 【Linux】快速清空当前文件

    $ : > filename $ > filename $ echo "" > filename $ echo > filename $ cat /dev/ ...

  6. 监控节点 xml写入数据库 存储过程

    USE [Appserver] GO /****** Object:  StoredProcedure [AppServer].[ImportNodeRelationData]    Script D ...

  7. C#编程的几个概念

    编译器(计算机) 开发人员 指令&对象 C# 有一批既定的指令, 关键字,方法.函数 变量.控件 通过指令去操作对象. 1. 熟记指令 2. 指令-对象的可操作关系 3. 合乎语法规范

  8. linux服务器时间自动同步

    最常用的是 rdate 服务 安装(centOs为例): yum install -y rdate 用法: rdate -s time-b.nist.gov 执行完以上方法时间就同步了.有的服务器隔一 ...

  9. Promise/A+规范学习总结

    Promise的实现:因为他只是一个规范,所以在不同的框架或者平台下有不同的实现 Angular:$q服务 Node:q模块,co,then Es6:Promise, yield Es7:async ...

  10. jQuery三——筛选方法、事件

    一.jquery常用筛选方法 以下为jquery的常用筛选方法: 代码示例如下: <!DOCTYPE html> <html lang="en"> < ...