Abandoned country

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5723

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

Source

2016 Multi-University Training Contest 1

##题意:

给出n个点m条边构成的图,求一个最小生成树,并且要求任意两点间距离和的期望最小.


##题解:

一开始看到期望感觉有点迷,不过题目里有一句话:任意两边不等.
这意味着最小生成树是唯一的,所以就不存在最小期望了.
这里用kruskal直接求最小生成树,把生成树中的边记录下来,再dfs跑一遍记录任意两点的距离和.
求平均距离和是直接抄的原题:HDU 2376
参考的博客:(http://www.cnblogs.com/wally/archive/2013/06/03/3116020.html)

WA:之前由于n*(n-1)没有考虑超longlong的情况,WA到爆炸. 归根结底还是代码习惯不好,真是弱到不行啊...
以后写代码应时刻注意操作数的范围.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 101000
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

struct node{

int left,right;

LL cost;

}road[maxn*10];

struct Node{

int v;

LL len;

};

vectorvet[maxn];

int n;

double dp[maxn];

LL sum[maxn];

int index[maxn10];

int order[maxn
10];

int cmp(node x,node y) {return x.cost<y.cost;}

int p[maxn],m;

int find(int x) {return p[x]=(p[x]==x? x:find(p[x]));}

LL kruskal()

{

LL ans=0;

for(int i=1;i<=n;i++) p[i]=i;

sort(road+1,road+m+1,cmp);

for(int i=1;i<=m;i++)

{

int x=find(road[i].left);

int y=find(road[i].right);

if(x!=y)

{

ans+=road[i].cost;

p[x]=y;

int u=road[i].left;

int v=road[i].right;

Node p1,p2;

p1.v=v,p2.v=u;

p1.len=p2.len=road[i].cost;

vet[u].push_back(p1);

vet[v].push_back(p2);

}

}

return ans;

}

bool vis[maxn];

void dfs(int root,int father){

sum[root]=1LL;

vis[root] = 1;

int sz = vet[root].size();

for(int i=0;i<sz;i++){

int son=vet[root][i].v;

LL len=vet[root][i].len;

if(vis[son])continue;

dfs(son,root);

sum[root]+=sum[son];

dp[root]+=dp[son]+(sum[son](n-sum[son]))(double)len;

}

}

int main(void)

{

//IN;

int t,i;
scanf("%d",&t);
while(t--)
{
memset(road,0,sizeof(road));
memset(index,0,sizeof(index));
memset(vis,0,sizeof(vis));
memset(order,0,sizeof(order));
scanf("%d %d",&n,&m);
for(i=1; i<=m; i++) {
scanf("%d %d %lld",&road[i].left,&road[i].right,&road[i].cost);
} for(int i=0;i<=n;i++) vet[i].clear();
memset(sum,0,sizeof(sum));
memset(dp,0,sizeof(dp));
LL ans=kruskal();
printf("%lld ",ans); dfs(1,-1); //double s=(((double)((double)n*(double)(n-1)))/2.0);
LL s = (LL)(n) * (LL)(n-1) / 2LL;
printf("%.2lf\n",(double)dp[1]/(double)s);
} return 0;

}

HDU 5723 Abandoned country (最小生成树 + dfs)的更多相关文章

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

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

  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 最小生成树 期望

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

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

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

  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 【最小生成树&&树上两点期望】

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

  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. C#使用sharppcap实现网络抓包

    sharppcap dll的下载地址: http://sourceforge.net/directory/os:windows/?q=sharppcap 具体使用详细步骤: http://www.co ...

  2. eclipse教程

    http://www.eclipse.org/downloads/eclipse-packages/http://wiki.eclipse.org/Eclipse_Articles,_Tutorial ...

  3. Java 基础之认识 Annotation

    Java 基础之认识 Annotation 从 JDK 1.5 版本开始,Java 语言提供了通用的 Annotation 功能,允许开发者定义和使用自己的 Annotation 类型.Annotat ...

  4. jQuery的威力

    jQuery如此之好用,和其在获取对象时使用与CSS选择器兼容的语法有很大关系,毕竟CSS选择器大家都很熟悉(关于CSS选择器可以看看十分钟搞定CSS选择器),但其强大在兼容了CSS3的选择器,甚至多 ...

  5. erl0003-ets 几种类型的区别和ets效率建议 <转>

    rlang内置大数据量数据库 ets,dets 初窥 发布日期:2011-10-24 18:45:48   作者:dp studio ets是Erlang term storage的缩写, dets则 ...

  6. poj 1201/zoj 1508 intervals 差分约束系统

      // 思路 : // 图建好后 剩下的就和上一篇的 火烧连营那题一样了 求得解都是一样的 // 所以稍微改了就过了 // 最下面还有更快的算法 速度是这个算法的2倍#include <ios ...

  7. Oracle 不同故障的恢复方案

    之前在Blog中对RMAN 的备份和恢复做了说明,刚看了下,在恢复这块还有知识点遗漏了. 而且恢复这块很重要,如果DB 真要出了什么问题,就要掌握对应的恢复方法. 所以把DB的恢复这块单独拿出来说明一 ...

  8. MyBatis的association示例——MyBatis学习笔记之三

    前两篇博文介绍的都是单表映射,而实际上很多时候我们需要用到较复杂的映射.今天学会的association的用法,就是一例,现写出来和大家分享(为简洁起见,ant工程中各文件.目录的布局,以及其它与前面 ...

  9. Android基于基于布局嵌套的页面导航实现

    页面如下: 主页面的布局分隔为三部分: 注意观察上面标记为红色的android:id均采用android系统默认的名称: 页面的导航组件: <?xml version="1.0&quo ...

  10. Easy Climb

    题意: 有n块石头,给定他们的高度,现保持第一和最后一块高度不变,其他可增加和减少高度,求通过变换使所有相邻石头的高度差的绝对值不大于d,所变化高度总和的最小值. 分析: 状态还可以想出来,dp[i] ...