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;
    }
  • 相关阅读:
    迭代器、生成器、内置函数
    函数
    文件操作
    使用 java 做爬虫的简单例子
    关于 spring 使用 mongodb 的 mongotemplate 对象操作数据库,对象注入问题(即该对象能否正常的调用相应的CRUD方法来处理数据)
    eclipse 使用 “全局搜索” 失灵的解决方法
    使用 Qrcode 生成中间带 logo 的二维码!
    将 BufferedImage 对象图片,转成 Base64 编码给前端<img src="编码"/>展示图片用
    使用 Qrcode 生成二维码
    链接转化成二维码
  • 原文地址:https://www.cnblogs.com/algorithms/p/2605281.html
Copyright © 2011-2022 走看看