zoukankan      html  css  js  c++  java
  • 暑假集训每日一题0723 (最小生成树)

    Description

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.

    Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.

    Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.

    The distance between any two farms will not exceed 100,000.

    Input

    There is several test cases.
    Line 1:     The number of farms, N (3 <= N <= 100).
    Line 2..end:     The subsequent lines contain the N x N connectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

    Output

    The single output contains the integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

    Sample Input

    4
    0 4 9 21
    4 0 8 17
    9 8 0 16
    21 17 16 0

    Sample Output

    28

    裸的最小生成树。

    View Code
    #include <stdio.h>
    #include <stdlib.h>
    #define N 110
    #define M 10100
    int n;
    int d[N][N];
    int p[N];
    struct node
    {
        int i,j,d;
    };
    node edge[M];
    int cnt;
    void make_set()
    {
        for(int i=0;i<n;i++)    p[i]=i;
    }
    int find_set(int i)
    {
        if(p[i]!=i) p[i]=find_set(p[i]);
        return p[i];
    }
    void union_set(int i,int j)
    {
        i=find_set(i);
        j=find_set(j);
        p[j]=i;
    }
    int cmp(const void *a,const void *b)
    {
        node *x=(node *)a;
        node *y=(node *)b;
        return (x->d)>(y->d)?1:-1;
    }
    int main()
    {
        int i,j,k,ans;
        while(~scanf("%d",&n))
        {
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)    scanf("%d",&d[i][j]);
            }
            cnt=0;
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    edge[cnt].i=i;
                    edge[cnt].j=j;
                    edge[cnt++].d=d[i][j];
                }
            }
            qsort(edge,cnt,sizeof(edge[0]),cmp);
            make_set();
            ans=0;
            for(k=0;k<cnt;k++)
            {
                i=edge[k].i;
                j=edge[k].j;
                if(find_set(i)^find_set(j)) ans+=edge[k].d,union_set(i,j);
            }
            printf("%d\n",ans);
        }
        return 0;
    }
  • 相关阅读:
    ByteBuffer的mark、position、limit、flip、reset,get方法介绍ok
    java.nio.ByteBuffer的flip、rewind和compact几个方法的区分使用
    maven之一:maven安装和eclipse集成
    Java 8 函数式接口
    Lambda 表达式
    jdk8十大新的特性
    阿里巴巴73款开源产品列表,值得收藏
    【Java】java.util.Objects 工具类方法研究
    ARIMA 模型简单介绍
    python 二维数组取值
  • 原文地址:https://www.cnblogs.com/algorithms/p/2605281.html
Copyright © 2011-2022 走看看