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

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到爆炸. 归根结底还是代码习惯不好,真是弱到不行啊...
以后写代码应时刻注意操作数的范围.


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#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;
};
vector<Node>vet[maxn];
int n;
double dp[maxn];
LL sum[maxn];

int index[maxn*10];
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;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5709526.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值