任意门: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. 引导篇之HTTP事务

    一个完整的HTTP事务流图: HTTP报文格式: 起始行:在请求报文中用来说明要做些什么,在响应报文中说明出现了什么情况 首部:起始行后面有0个或多个首部字段.每个首部字段都包含一个名字和一个值,为了 ...

  2. HikariCP配置使用spring结合--Java数据库连接池

    我的个人德州扑克项目https://github.com/mingzijian/pokers,欢迎给星星.maven引入: Java 8 maven artifact: <dependency& ...

  3. java多态简单例子

    /* 对象的多态性:动物 x = new 猫(); 函数的多态性:函数重载.重写 1.多态的体现 父类的引用指向了自己的子类对象 父类的引用也可以接收自己的对象 2.多态的前提 必须是类与类之间只有关 ...

  4. [linux]解决DNS配置重启丢失

    DNS配置重启丢失 每次重启后都修改DNS配置文件 /etc/resolv.conf从网上得知 /etc/resolv.conf中的DNS配置是从/etc/resolvconf/resolv.conf ...

  5. tomcat修改jvm内存

    内存大小:-Xms256M -Xmx512M -XX:PermSize=256m -XX:MaxNewSize=256m -XX:MaxPermSize=512m -Djava.awt.headles ...

  6. java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE 的理解

    [2013-12-06 11:06:21,715] [C3P0PooledConnectionPoolManager[identityToken->2tl0n98y1iwg7cbdzzq7a|7 ...

  7. 关于“System.Data.ProviderIncompatibleException”类型的异常

    作为刚学习MVC的菜鸟,因为平常的不努力学习.看书,所以只能参考官方的教程学习操作新手入门 一步一步认真的做,前天晚上出现了一个关于数据库连接字符串错误的问题,自己查了很多资料, 问了许多大神,他们的 ...

  8. vue——指令系统

    指令系统,可以联想咱们的cmd命令行工具,只要我输入一条正确的指令,系统就开始干活了. 在vue中,指令系统,设置一些命令之后,来操作我们的数据属性,并展示到我们的DOM上. 在vue中提供了一套为数 ...

  9. keras 自定义 custom 函数

    转自: https://kexue.fm/archives/4493/,感谢分享! Keras是一个搭积木式的深度学习框架,用它可以很方便且直观地搭建一些常见的深度学习模型.在tensorflow出来 ...

  10. 生成对抗式网络 GAN的理解

    转自:https://zhuanlan.zhihu.com/p/24767059,感谢分享 生成式对抗网络(GAN)是近年来大热的深度学习模型.最近正好有空看了这方面的一些论文,跑了一个GAN的代码, ...