Abandoned country

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

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
 
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue;
#define PF printf
#define SC scanf
const int mod=1000000007;
const int N=1e6+10;
int num[N],f[N];
vector<int> G[N]; struct edge{
int u,v,cost,flag;
}e[N]; bool cmp(edge a,edge b)
{
return a.cost<b.cost;
} int findr(int u)
{
if(f[u]!=u)
f[u]=findr(f[u]);
return f[u];
} int dfs_clock; void dfs(int u,int pre)
{
int p=++dfs_clock;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(v==pre) continue;
dfs(v,u);
}
num[u]=dfs_clock-p+1;
} int main()
{
int cas,n,m;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) G[i].clear();
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].cost);
e[i].flag=0;
}
sort(e+1,e+m+1,cmp);
for(int i=1;i<=n;i++) f[i]=i;
ll cost=0;
for(int i=1;i<=m;i++)
{
int u=findr(e[i].u),v=findr(e[i].v);
if(u==v) continue;
f[u]=v;
e[i].flag=1;
cost+=e[i].cost;
G[e[i].u].push_back(e[i].v);
G[e[i].v].push_back(e[i].u);
} dfs_clock=0;
dfs(1,-1);
double q=0;
for(int i=1;i<=m;i++)
if(e[i].flag)
{
int u=e[i].u,v=e[i].v;
ll k=min(num[u],num[v]);
q+=k*(n-k)*e[i].cost;
} q=2*q/n/(n-1);
printf("%lld %.2f\n",cost,q);
}
return 0;
}

  分析:刚开始以为是道kruskal+统计子节点的水题,,,,后来写了下,,发现这道题目有个很迷的地方,就是连接起所有村庄的最小的总cost和最小的期望值,最小的cost当然跑kruskal,那会不会在同一种

cost的情况下出现不同期望值?那该怎么选择,,,于是瞬间懵逼,,就放弃了。。。

可见,,比赛时缺乏基本的随机应变和见招拆招的能力,,这要靠比赛来加强了。。。。比赛时胆子真的太小了,,,只有多多比赛慢慢克服了。。。

解决:其实因为题目说了所有边的权值均不同,,kruskal中对边sort后,形成最小生成树的边的选取方案是

唯一的,所以最小生成树唯一;

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 (最小生成树+dfs)

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

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

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

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

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

  6. HDU 5723 Abandoned country 【最小生成树&&树上两点期望】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5723 Abandoned country Time Limit: 8000/4000 MS (Java/ ...

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

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

  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(kruskal+dp树上任意两点距离和)

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

随机推荐

  1. print格式化输出(format)

    一. print格式化输出,以及使用format控制 字符串的格式化方法分为两种,分别为占位符(%)和format方式.占位符方式在Python2.x中用的比较广泛,随着Python3.x的使用越来越 ...

  2. Elastic Search对Document的搜索

    在ES中使用的重点.ES中存储的数据.核心就是为了提供全文搜索能力的.搜索功能非常重要.多练. 1 query string searchsearch的参数都是类似http请求头中的字符串参数提供搜索 ...

  3. Asp.net core Enum as string + ef core value convertor

    更新 : 2019-06-08 build in convertor https://docs.microsoft.com/en-us/ef/core/modeling/value-conversio ...

  4. IIS--解决64位系统IIS网站发布出现未能加载文件或程序集“...”或它的某一个依赖项。试图加载

    解决64位系统IIS网站发布出现未能加载文件或程序集“...”或它的某一个依赖项.试图加载 ASP.NET MVC 项目发布的在本地IIS后,启动网站出现未能加载文件或程序集“…”或它的某一个依赖项. ...

  5. centos7安装配置NFS文件共享存储

    一,环境介绍    本实验使用了两台centos7虚拟机,其中         服务器:192.168.1.188    客户端:192.168.1.189 二,实验步骤    192.168.1.1 ...

  6. Java必考题目之JVM面试题目和答案

    JVM内存模型 首先我们来了解一下JVM的内存模型的怎么样的: 1.堆:存放对象实例,几乎所有的对象实例都在这里分配内存 堆得内存由-Xms指定,默认是物理内存的1/64:最大的内存由-Xmx指定,默 ...

  7. Android Jetpack之WorkManager: 观察结果

    在使用WorkManager的时候,有时候需要关注任务执行的结果和状态,可以使用 LiveData<WorkInfo> liveOpStatus = WorkManager.getInst ...

  8. 关于IDEA导入依赖问题,阿里云下载不了

    关于阿里云,有部分数据是不能够下载的,就拿ojdbc8-12.2.0.1.0.jar来说 pom.xml <!--Oracle驱动 因为maven仓库下载不了,采用本地导入--> < ...

  9. SQL 语句 连接

    SQL连接可以分为内连接.外连接.交叉连接. 数据库数据:             book表                                          stu表 1.内连接 ...

  10. MySQL查询数据库中所有数据表的数据条数

    select table_name,table_rows from information_schema.tables where TABLE_SCHEMA = '数据库名称' order by ta ...