Abandoned country

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

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
/*
HDU 5723 Abandoned country 最小生成树+搜索 problem:
给你n个点和m条边,让你求最少花费多少可以将所有点连通并求出任意两点的花费期望 solve:
第一个直接求最小生成树。主要是不懂它这个期望到底要求什么。看题解说的是深搜求出每条路用过
的次数来得到总花费。然后除以可能发生的次数 by——hhh
*/
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <map>
#define lson ch[r][0]
#define rson ch[r][1]
#define ll long long
#define key_val ch[ch[root][1]][0]
using namespace std;
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
int vis[maxn];
int f[maxn];
vector<pair<int,int>> q[maxn];
struct Edge
{
int u,v,w;
} edge[1000010]; int tot; void add(int u,int v,int val)
{
edge[tot].u = u,edge[tot].v = v,edge[tot++].w = val;
} bool cmp(Edge a,Edge b)
{
return a.w < b.w;
} int fin(int x)
{
if(f[x] == -1) return x;
return f[x] = fin(f[x]);
} ll cal(int n)
{
memset(f,-1,sizeof(f));
sort(edge,edge+tot,cmp);
ll cnt = 0,ans = 0;
for(int i = 0; i < tot; i++)
{
int u = edge[i].u;
int v = edge[i].v;
int w = edge[i].w;
int t1 = fin(u),t2 = fin(v);
if(t1 != t2)
{
ans = (ll)(ans + w);
f[t1] = t2;
cnt++;
q[u].push_back(make_pair(v,w));
q[v].push_back(make_pair(u,w)); }
if(cnt == n-1)
break;
}
// cout << cnt <<endl;
return ans;
}
int n;
double ans;
ll dfs(int now)
{
vis[now] = 1;
ll t = 0,ta = 0;
for(int i = 0; i < q[now].size(); i++)
{
ll v = q[now][i].first;
ll w = q[now][i].second;
if(!vis[v])
{
t = dfs(v);
ta += t;
ans = ans+1.0*t*(n-t)*w;
}
}
return ta+1;
} int main()
{
int T,a,c,b;
// freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
int m;
ll tans;
tot = 0,ans = 0;
memset(vis,0,sizeof(vis));
scanf("%d%d",&n,&m); for(int i =0; i <= n; i++)
q[i].clear();
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
if(!n || !m)
{
printf("0 0.00\n");
continue;
}
tans = cal(n);
dfs(1);
double t = (1.0*n*(n-1)/2);
// cout <<ans <<" " <<t<<endl;
printf("%I64d %.2f\n",tans,ans/t);
}
return 0;
}

  

HDU 5723 Abandoned country 最小生成树+搜索的更多相关文章

  1. hdu 5723 Abandoned country 最小生成树 期望

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

  2. hdu 5723 Abandoned country 最小生成树+子节点统计

    Abandoned country Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  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. DNS协议(一)

    在互联网上要想与另外一台主机通信,要知道对方的IP地址,但是IP地址是很难记忆的, 比如百度的一台服务器的IP地址为115.239.210.27,我们在浏览器中输入http://115.239.210 ...

  2. 20162328蔡文琛week05

    学号 20162328 <程序设计与数据结构>第X周学习总结 教材学习内容总结 面向对象程序设计的核心是类的定义,它代表定义了状态和行为的对象. 变量的作用域依赖于变量声明的位置,作用域决 ...

  3. 使用Python定制词云

    一.实验介绍 1.1 实验内容 在互联网时代,人们获取信息的途径多种多样,大量的信息涌入到人们的视线中.如何从浩如烟海的信息中提炼出关键信息,滤除垃圾信息,一直是现代人关注的问题.在这个信息爆炸的时代 ...

  4. 团队作业4——第一次项目冲刺(Alpha版本)2017.11.16

    第一次会议:2017-11-16 大家的任务完成的不错^_^,继续努力了. 上图: 忘记照了,额....... 会议主要内容: 1.登录功能的讨论 2. 代码统一 具体分工: 成员 计划任务 遇见难题 ...

  5. 2017北京国庆刷题Day1 afternoon

    期望得分:100+100+100=300 实际得分:100+100+100=300 T1 一道图论好题(graph) Time Limit:1000ms   Memory Limit:128MB 题目 ...

  6. MSSQL 2000 错误823恢复案例

    一.故障描述 MSSQL Server 2000 附加数据库错误823,附加数据库失败.数据库没有备份,不能通过备份恢复数据库,急需恢复数据库中的数据. 二.故障分析SQL Server数据库 823 ...

  7. vue jquery js 获取当前时间本周的第一天 和 本月的第一天

    交互的时候传输数据 后台要求这样的数据 直接上代码 这是我找度姨要的  附上链接  https://www.cnblogs.com/wasabii/p/7756560.html 它里面有本季度第一天  ...

  8. WebApi一个控制器中定义多个Get方法。

    问题:怎样解决一个ApiController中定义多个Get方法或者Post方法? 答:要想实现一个ApiController中定义多个Get方法或者Post方法,则需要在WebApiConfig类中 ...

  9. mosquitto安装和测试

    一.安装 1.windows安装 安装完毕,更新安装目录的dll文件 2.linux安装 编译保存用户数据到数据库的插件 安装 3.启动 mosquitto mosquitto mosquitto_p ...

  10. Android fragment切换后onresume时报 Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim'

    动态加载fragment以后,调用了remove方法移除Fragment,在返回来的时候报 Attempt to write to field 'int android.support.v4.app. ...