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;
    }
  • 相关阅读:
    聊聊HTTP gzip压缩与常见的Android网络框架
    Material适配2
    Material适配1
    将Eclipse代码导入到AndroidStudio的两种方式
    Android批量打包提速
    放弃WebView,使用Crosswalk做富文本编辑器
    Android MP3录音实现
    OkHttp2.0有Bug,暂时不推荐在产品中使用
    Java XML SAX 解析注意
    Evernote Markdown Sublime使用介绍
  • 原文地址:https://www.cnblogs.com/yu0111/p/5687204.html
Copyright © 2011-2022 走看看