zoukankan      html  css  js  c++  java
  • Agri-Net

    Agri-Net

    http://poj.org/problem?id=1258

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 69658   Accepted: 28861

    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

    Source

    模板题

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 using namespace std;
     6 
     7 int mp[105][105];
     8 struct sair{
     9     int x,y,dis;
    10 }p[100005];
    11 
    12 bool cmp(sair a,sair b){
    13     return a.dis<b.dis;
    14 }
    15 
    16 int fa[100005];
    17 int Find(int x){
    18     int r=x,y;
    19     while(x!=fa[x]){
    20         x=fa[x];
    21     }
    22     while(r!=x){
    23         y=fa[r];
    24         fa[r]=x;
    25         r=y;
    26     }
    27     return x;
    28 }
    29 
    30 bool join(int x,int y){
    31     int xx=Find(x);
    32     int yy=Find(y);
    33     if(xx!=yy){
    34         fa[xx]=yy;
    35         return true;
    36     }
    37     return false;
    38 }
    39 
    40 int main(){
    41     std::ios::sync_with_stdio(false);
    42     int n;
    43     while(cin>>n){
    44         int co=0;
    45         for(int i=0;i<=n;i++) fa[i]=i;
    46         for(int i=1;i<=n;i++){
    47             for(int j=1;j<=n;j++){
    48                 cin>>mp[i][j];
    49                 p[co].x=i,p[co].y=j,p[co++].dis=mp[i][j];
    50             }
    51         }
    52         sort(p,p+co,cmp);
    53         int ans=0;
    54         for(int i=0;i<co;i++){
    55             if(join(p[i].x,p[i].y)){
    56                 ans+=p[i].dis;
    57             }
    58         }
    59         cout<<ans<<endl;
    60     }
    61 }
    View Code
  • 相关阅读:
    springboot整合Swagger2
    FastJson会把哪些字符串解析为null
    BitMap再再体验之布隆过滤器
    如何利用windows自带的画图工具拼接图片
    BitMap再体验之排序
    BitMap 初体验
    Chrome 隐藏最常访问的网站
    idea同一个项目不同端口启动
    ubuntu16搭建harbor镜像库
    virtualbox硬盘扩容
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9968710.html
Copyright © 2011-2022 走看看