HDU 5723 Abandoned country【2016多校联合】(最小生成树+深搜)

61 篇文章 0 订阅
30 篇文章 1 订阅

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): 106 Accepted Submission(s): 36

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

Source
2016 Multi-University Training Contest 1

Recommend
wange2014

思路:先用最小生成树把所有边找出,然后用深搜实现查找每个边用了多少次。

下面是AC代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
typedef long long ll;
using namespace std;
const int N = 200005;
int ss;
struct node
{
    int uu,vv;
    ll cost;
} c[1000005],b[100005];

int pre[100005];

int fin(int x)
{
    if(x==pre[x])
    {
        return x;
    }
    else
    {
        return pre[x]=fin(pre[x]);
    }
}

void join(int x,int y)
{
    int t1=fin(x);
    int t2=fin(y);
    if(t1!=t2)
    {
        pre[t1]=t2;
    }
}

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


int t, n, firs[N], nex[N], u[N], v[N], E, vis[N];
ll w[N], tim[N];

void add(int a, int b, ll value)
{
    u[E] = a;
    v[E] = b;
    w[E] = value;
    tim[E] = 0;
    nex[E] = firs[u[E]];
    firs[u[E]] = E;
    E ++;
}

void init()
{
    memset(vis, 0, sizeof(vis));
    E = 0;
    memset(firs, -1, sizeof(firs));
    for (int i = 0; i < ss; i ++)
    {
        add(b[i].uu, b[i].vv, b[i].cost);
        add(b[i].vv, b[i].uu, b[i].cost);
    }
}

ll dfs(int u)
{
    ll t = 0;
    vis[u] = 1;
    for (int e = firs[u]; e != -1; e = nex[e])
    {
        if (vis[v[e]]) continue;
        ll cnt = dfs(v[e]);
        t += cnt;
        tim[e] = tim[e^1] = cnt * (n - cnt);
    }
    return t + 1;
}

double solve()
{
    double ans = 0.0;
    dfs(0);
    for (int i = 0; i < E; i ++)
    {
        ans += (tim[i] * w[i])*1.0;
    }
    return ans;
}

int main()
{
    int T,m;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            pre[i]=i;
        }
        for(int i=0; i<m; i++)
        {
            scanf("%d%d%I64d",&c[i].uu,&c[i].vv,&c[i].cost);
        }
        if(n==1)
        {
            printf("0 0.00\n");
            continue;
        }
        sort(c,c+m,cmp);
        ss=0;
        ll sum=0;
        for(int i=0; i<m; i++)
        {
            if(fin(c[i].uu)!=fin(c[i].vv))
            {
                join(c[i].uu,c[i].vv);
                sum+=c[i].cost;
                b[ss].uu=c[i].uu-1,b[ss].vv=c[i].vv-1,b[ss].cost=c[i].cost;
                ss++;
            }
            if(ss==n-1)
            {
                break;
            }
        }
        init();
        double re=solve();
        double ac=re/double(n*1.0);
        ac=ac/(double((n-1)*1.0));
        printf("%I64d %.2lf\n",sum,ac);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值