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;
    }
  • 相关阅读:
    python学习笔记(1)
    一些有趣的使用function
    axios构建请求池处理全局loading状态&&axios避免重复请求
    axios构建缓存池存储基础数据
    文件下载方法
    关于 JS this
    前端 JS 获取 Image 图像 宽高 尺寸
    Html CSS transform matrix3d 3D转场特效
    Github 持续化集成 工作流 Npm包自动化发布
    远程 Linux(Ubuntu 18)添加字体
  • 原文地址:https://www.cnblogs.com/algorithms/p/2605281.html
Copyright © 2011-2022 走看看