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

177 篇文章 0 订阅
91 篇文章 1 订阅

Abandoned country

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


Problem Description
An abandoned country has n(n100000) villages which are numbered from 1 to n . Since abandoned for a long time, the roads need to be re-built. There are m(m1000000) roads to be re-built, the length of each road is wi(wi1000000) . 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(T10) 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
 
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723

题目大意:给一个图,求一个最小生成树,输出权值和这个最小生成树任意两点间距离的平均值

题目分析:签到题,和hdu 2376一样,只不过这题要在求出来的MST上求解,sort可能会T,因为是3990ms过的,或者因为权值不一样,直接枚举权值做2500ms过,各种爆int

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int const MAXN = 1e5 + 5;
int const MAXM = 1e6 + 5;
int n, m;

struct EDGE
{
    int to, nxt;
    ll w;
}e[MAXM << 1];
int head[MAXN], cnt, fa[MAXN];
int num[MAXN];
ll sum;

struct EDGE2
{
    int u, v;
}e2[MAXM];

void Add(int u, int v, ll w)
{
    e[cnt].to = v;
    e[cnt].w = w;
    e[cnt].nxt = head[u];
    head[u] = cnt ++;

    e[cnt].to = u;
    e[cnt].w = w;
    e[cnt].nxt = head[v];
    head[v] = cnt ++;
}

void Init()
{
    for(int i = 0; i <= n; i++)
        fa[i] = i;
}

int Find(int x) 
{
    return x == fa[x] ? x : fa[x] = Find(fa[x]);
}

void Union(int a, int b)
{
    int r1 = Find(a);
    int r2 = Find(b);
    if(r1 != r2)
        fa[r2] = r1;
}

void DFS(int u, int fa)  
{  
    num[u] = 1;  
    for(int i = head[u]; i != -1; i = e[i].nxt)  
    {     
        int v = e[i].to;  
        ll w = e[i].w;  
        if(v != fa)  
        {  
            DFS(v, u);  
            num[u] += num[v];  
            sum += (ll)num[v] * (ll)(n - num[v]) * w;  
        }  
    }  
}  

int main()
{
    int T;
    scanf("%d", &T);
    while(T --)
    {
        scanf("%d %d", &n, &m);
        Init();
        cnt = 0;
        sum = 0;
        memset(head, -1, sizeof(head));
        memset(e2, 0, sizeof(e2));
        memset(num, 0, sizeof(num));
        int ma = 0;
        for(int i = 0; i < m; i++)
        {
            int u, v, w;
            scanf("%d %d %d", &u, &v, &w);
            ma = max(ma, w);
            e2[w].u = u;
            e2[w].v = v;
        }
        ll ans = 0;
        int pnum = 0;
        for(int i = 0; i <= ma; i++)
        {
            if(e2[i].u && e2[i].v)
            {
                int uu = e2[i].u;
                int vv = e2[i].v;
                if(Find(uu) != Find(vv))
                {
                    pnum ++;
                    ans += (ll) i;
                    Union(uu, vv);
                    Add(uu, vv, i);
                    if(pnum >= n - 1)
                        break;
                }
            }
        }
        DFS(1, -1);
        ll tmp = (ll) n * (ll) (n - 1) / 2ll;
        printf("%I64d %.2f\n", ans, (double) sum / (double) tmp);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值