• 暑假集训每日一题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;
    }
  • 相关阅读:
    Kafka 集群搭建 (自用)
    Kafka 简单实验二(Python实现简单生产者消费者)
    Kafka 简单实验一(安装Kafka)
    Spark下载与入门(Spark自学二)
    Spark导论(Spark自学一)
    MongoDB分片集群新增分片(自用)
    Python 获取图片文件大小并转换为base64编码
    Python 获取图片格式及像素宽高信息
    MongoDB集群设置集合分片生效及查看集合分片情况
    商城购物车的实现设计思想
  • 原文地址:https://www.cnblogs.com/algorithms/p/2605281.html
Copyright © 2020-2023  润新知