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

    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
     1 #include <stdio.h>
     2 #include <string.h>
     3 #define V 256
     4 
     5 #define typec int
     6 const typec inf = 0x7fffffff; 
     7 int vis[V];            
     8 typec lowc[V];    
     9 
    10 typec prim(typec cost[][V], int n) 
    11 {
    12     int i, j, p;
    13     typec minc, res = 0;
    14     memset(vis, 0, sizeof(vis));
    15     vis[0] = 1;
    16     for (i=1; i<n; i++) lowc[i] = cost[0][i];
    17     for (i=1; i<n; i++) {
    18         minc = inf; p = -1;
    19         for (j=0; j<n; j++)
    20             if (0 == vis[j] && minc > lowc[j]) {
    21                 minc = lowc[j]; p = j;
    22             }
    23         if (inf == minc) return -1; 
    24         res += minc; vis[p] = 1;
    25         for (j=0; j<n; j++)
    26             if (0 == vis[j] && lowc[j] > cost[p][j])
    27                 lowc[j] = cost[p][j];
    28     }
    29     return res;
    30 }
    31 
    32 int matrix[V][V];
    33 
    34 int main(){
    35     int n,i,j;
    36     while((scanf("%d",&n) == 1) && (n>=3 && n<=100) ){
    37         for(i=0;i<n;i++){
    38             for(j=0;j<n;j++){
    39                 scanf("%d",&matrix[i][j]);
    40             }
    41         }
    42 
    43         int res = prim(matrix,n);
    44         if(-1 != res){
    45             printf("%d
    ",res);
    46         }
    47     }
    48 
    49 }

     

  • 相关阅读:
    Js 循环 forEach, map, for, for...in, for...of
    es6 let const
    es6 Symbol类型
    es6 Reflect 与 Proxy
    es6 Map&Set
    es6箭头函数
    es6数组Arrary
    学写网站(一)前端配置之安装nvm、node、npm
    python获取当前执行代码所在文件的地址、主程序所在地址
    scrapy中的选择器下载中间件downloadmiddlewares
  • 原文地址:https://www.cnblogs.com/lvyahui/p/4009943.html
Copyright © 2011-2022 走看看