zoukankan      html  css  js  c++  java
  • hdu 5723 Abandoned country(最小生成树(Kruskal)+dfs)

    /*
    A.Abandoned country
    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

    题目大意:
    1.有n个点,m条边,需要建立一个图,使得花费最小并且保证每个点之间有路通达,求最小花费。
    2.求其最小期望值(期望=所有村庄中任意两个村庄距离之和/村庄对数(n*(n-1)/2)。


    类型:
    最小生成树(Kruskal)+dfs


    思路:
    先用Kruskal建立最小生成树(N^2太大,Prim吃不消),并将其路径存储在th[]中,利用head[]记录节点数,
    那么对应每一条边,其对应选择通过的次数为:tmp*(n-tmp),然后就是dfs一遍出答案

    */

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const LL N = 1000010;
    const LL INF = 0x3f3f3f3f;
    LL father[N],head[N];
    bool vis[N];
    LL n,m,cont;
    double sum=0;
    typedef struct
    {
        LL u,v,w;
    } BY;
    typedef struct
    {
        LL from,to,next,w;
    } TH;
    TH th[N];
    BY ed[N];
    bool cmp(BY a,BY b)
    {
        return a.w<b.w;
    }
    LL find(LL x)
    {
        return x==father[x]?x:father[x]=find(father[x]);
    }
    void add(LL from , LL to ,LL w)
    {
        th[cont].to=to;
        th[cont].w=w;
        th[cont].next=head[from];
        head[from]=cont++;
    }
    LL dfs(LL x)
    {
        vis[x]=1;
        LL num=1,tmp=0;
        for(LL i=head[x]; i!=-1; i=th[i].next) {
            LL t=th[i].to;
            LL w=th[i].w;
            if(!vis[t]) {
                tmp=dfs(t);
                sum+=tmp*(n-tmp)*w*1.0;
                num+=tmp;
            }
        }
        return num;
    }
    LL Kruskal()
    {
        LL i=0,ans=0;
        for(i=0; i<=n; i++)
            father[i]=i;
        sort(ed,ed+m,cmp);
        for(i=0; i<m; i++) {
            LL a,b;
            a=find(ed[i].u);
            b=find(ed[i].v);
            if(a!=b) {
                father[b]=a;
                add(ed[i].u,ed[i].v,ed[i].w);
                add(ed[i].v,ed[i].u,ed[i].w);
                ans+=ed[i].w;
            }
        }
        return ans;
    }
    int main()
    {
        LL T,i;
        cin>>T;
        while(T--) {
            sum=cont=0;
            scanf("%lld%lld",&n,&m);
            memset(head,-1,sizeof(head));
            memset(vis,false,sizeof(vis));
            for(i=0; i<m; i++)
                scanf("%lld%lld%lld",&ed[i].u,&ed[i].v,&ed[i].w);
            LL ans=Kruskal();
            dfs(1);
            printf("%lld %.2lf
    ",ans,sum*2.0/(n*(n-1)*1.0));
        }
        return 0;
    }
  • 相关阅读:
    unity3d 打包个人记录
    Unity3d 协程
    Unity3d 复制文字到剪切板及存储图像到相册
    为什么 redo log 具有 crash-safe 的能力,是 binlog 无法替代的?
    Java源码系列4——HashMap扩容时究竟对链表和红黑树做了什么?
    深入理解Java虚拟机1——内存区域
    Java源码系列3——LinkedHashMap
    深入理解Java虚拟机2——对象探秘
    高效学习
    WordPress自动代码高亮
  • 原文地址:https://www.cnblogs.com/yu0111/p/5687204.html
Copyright © 2011-2022 走看看