zoukankan      html  css  js  c++  java
  • 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;
    }
    
    


  • 相关阅读:
    SQL语句优化(转)
    ConcurrentHashMap源码分析
    centos 上安装gearman
    从与UCenter集成的过程中,看到Discuz的不开放
    UCenter实现同步登陆
    讨人喜欢的 MySQL replace into 用法(insert into 的增强版)
    Truncate Table
    mysql中key 、primary key 、unique key 与index区别
    PHP登录时限
    用Fragment实现如新浪微博一样的底部菜单的切换
  • 原文地址:https://www.cnblogs.com/mingrigongchang/p/6246257.html
Copyright © 2011-2022 走看看