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;
    }
    
    


  • 相关阅读:
    jquery 获取easyui combobox选中的值
    一个多余逗号引起的麻烦
    Microsoft.Office.Interop.Excel 放到B/S客户端失败问题 检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问。
    自己收藏-javascript用window.open的子窗口关闭自己并且刷新父窗口
    easyUI datagrid 不刷新问题
    水晶报表中公式字段if else 语句无法正常执行的问题
    SQL SERVER 察看数据库连接池情况
    Data Table 转 List<Type>
    .Net 调用SAP RFC
    VS2017 插件介绍
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256408.html
Copyright © 2011-2022 走看看