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

     

  • 相关阅读:
    动态规划之背包问题
    Python中import导入上一级目录模块及循环import问题的解决
    Anaconda介绍、安装及使用教程
    负载均衡基础知识
    TCP和UDP的区别(转)
    microsoft visual c++ 14.0 is required问题解决办法
    python使用requests时报错requests.exceptions.SSLError: HTTPSConnectionPool
    解决Anaconda无法更新的问题
    彻底的理解TCP协议的三次握手和四次分手
    android调试工具 adb命令学习
  • 原文地址:https://www.cnblogs.com/lvyahui/p/4009943.html
Copyright © 2020-2023  润新知