任意门: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. sed命令——批量修改文件内容

    批量替换单个文件内容 命令格式:sed -i 's/旧内容/新内容/g' 文件路径 sed -i 's/oldString/newString/g' file 例如:我想替换cwx.txt文件中的 j ...

  2. API-Framework 前后端分离

  3. 阿里云centos 7 中tomcat 自启动

    这里我的tomcat的安装路径为 /usr/local/tomcat 1 为tomcat添加自启动参数 catalina.sh在执行的时候会调用同级路径下的setenv.sh来设置额外的环境变量,因此 ...

  4. GoLang爬取花瓣网美女图片

    由于之前一直想爬取花瓣网(http://huaban.com/partner/uc/aimeinv/pins/) 的图片,又迫于没时间,所以拖了很久. 鉴于最近在学go语言,就刚好用这个练手了. 预览 ...

  5. 【数据库】Oracle中删除新建并授权用户

    DROP USER fengw_110 CASCADE; CREATE USER fengw_110 IDENTIFIED BY root123; grant connect,resource,cre ...

  6. IE678不兼容CSS3 user-select:none(不可复制功能),需要JS解决

    [方法一:CSS3实现文本不可复制] .content {-moz-user-select:none;-webkit-user-select:none;-ms-user-select:none;-o- ...

  7. WAMP环境配置-Mysql安装

    1.下载并解压MySQL5.6.36压缩包(顺便重命名一下子). 2.将my-default.ini文件复制一份改名为my.ini,然后修改下面红框标注的地方 3.安装与启动服务. 以管理员的身份运行 ...

  8. lua load

    load (chunk [, chunkname [, mode [, env]]]) 加载一个代码块. 如果 chunk 是一个字符串,代码块指这个字符串. 如果 chunk 是一个函数, load ...

  9. 学习 JavaScript 树

    学习 JavaScript 树 树(Tree) 在计算机科学中,数据结构是一门研究非数值计算的程序设计问题中计算机的操作对象(数据元素)以及它们之间的操作和运算等的学科. 它包含三方面的内容: 数据的 ...

  10. Javascript 自定义输出

    缘由 前段时间再看了一些javascript的学习资料,也写的一些demo,在输出的时候一般都用alert,但这个方法会打断函数运行,用起来不是很好.还有就是console.log这个方法,这种方法原 ...