• POJ 1258 城市的道路建设


    都是套路,换题不换意

    Agri-Net
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 51655   Accepted: 21544

    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

    The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity 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

    For each case, output a single 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


    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    struct edge
    {
        int a,b;
        int w;
        bool operator<(const edge&wakaka)const
        {
            return w<wakaka.w;
        }
    }city[100000];
    int flag[100000];
    int find(int c)
    {
        int t=c;
        while(t!=flag[t])
            t=flag[t];
        int x=c,i;
        while(flag[x]!=t)
        {
           i=flag[x];
           flag[x]=t;
           x=i;
        }
        return t;
    }
    void marry(int c1,int c2)
    {
        if(c1>c2)
            flag[c1]=c2;
        else
            flag[c2]=c1;
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            int cnt=0;
            int i,j;
            int way;
            for(i=1;i<=n;i++)
                for(j=1;j<=n;j++)
                    {
                        scanf("%d",&way);
                        if(i<j)
                        {
                            city[++cnt].a=i;
                            city[cnt].b=j;
                            city[cnt].w=way;
                        }
                    }
            for(i=1;i<=n;i++)
                flag[i]=i;
            sort(city+1,city+1+cnt);
            int ans=0;
            for(i=1;i<=cnt;i++)
            {
                int x1=find(city[i].a);
                int x2=find(city[i].b);
                if(x1!=x2)
                {
                    marry(x1,x2);
                    ans=ans+city[i].w;
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    
    


  • 相关阅读:
    C++ 引用的作用和用法
    const和指针
    sizeof的用法
    C++数组初始化的问题
    C++变量的存储类别(动态存储、静态存储、自动变量、寄存器变量、
    c++变量在内存中的存储区域(转)
    C语言的 &数组名 和 数组名的区别
    C语言运算中的数据类型自动转换原则
    链表反转
    常见的排序方法
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256408.html
Copyright © 2020-2023  润新知