任意门: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. Element UI 中国省市区级联数据

    https://www.npmjs.com/package/element-china-area-data

  2. Tortoise SVN 快速操作手册

    1.库的存储结构 版本库文件结构如图所示: Code文件夹为源码文件夹,doc为文档目录文件夹, 1.1 branch:分枝文件夹 当多个人合作,可能有这样的情况出现:John突然有个想法,跟原先的设 ...

  3. Http编程之HttpClient

    在Android开发中,Android SDK附带了Apache的HttpClient,它是一个完善的客户端.它提供了对HTTP协议的全面支持,可以使用HttpClient的对象来执行HTTP GET ...

  4. VS中为什么不同的项目类型属性查看和设置的界面不一样

    在VS中,存在ATL.MFC.Win32.CLR.常规等等各种工程模板,这些工程模板对应于开发不同类型的应用,比如要开发com,你应该选ATL:开发最原始的通过API代用操作系统的应用,应该用Win3 ...

  5. 如何在ThinkPHP中开启调试模式

    1.为什么使用调试模式? 因为调试会在我们的模板页的最后增加一些trace信息. 2.什么是trace信息? 是ThinkPHP为我们提供好的一些包含了系统运行时间.占用内存.加载时间.请求的协议.. ...

  6. Coursera 机器学习 第7章 Support Vector Machines 学习笔记

    7 Support Vector Machines7.1 Large Margin Classification7.1.1 Optimization Objective支持向量机(SVM)代价函数在数 ...

  7. 安装VMware,出现Microsoft Runtime DLL 安装程序未能完成安装,解决方法

    安装VMware Workstation 12 Player出现如下问题: 解决方法: 1.出现这个问题的时候不要点确定(如果点了确定,会找不到步骤4中的文件夹) 2.win+R调出 '运行' 3.输 ...

  8. Use the list and while to Build Shop car

    #Author: Gordonsalary = int(input("请输入你的工资:"))goods = [('0',"Iphone",5000),('1', ...

  9. 【Linux】安装配置JDK1.8

    第一步:下载Linux环境下的jdk1.8,请去(官网)中下载jdk的安装文件: https://www.oracle.com/technetwork/java/javase/downloads/in ...

  10. PAT 1066 Root of AVL Tree

    #include <cstdio> #include <cstdlib> class Node { public: Node* L; Node* R; int height; ...