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 }

     

  • 相关阅读:
    1101. Quick Sort (25)
    1100. Mars Numbers (20)
    1099. Build A Binary Search Tree (30)
    TCP四次挥手为何需要TIME_WAIT以及为何是2MSL?
    关于priority_queue运算符重载的问题
    leetcode151.翻转字符串里的单词
    华为笔试题--最长公共子串
    华为笔试题--表达式求值
    华为笔试题--字符串合并处理
    华为笔试题--删除字符串中出现次数最少的字符
  • 原文地址:https://www.cnblogs.com/lvyahui/p/4009943.html
Copyright © 2011-2022 走看看