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 }

     

  • 相关阅读:
    文本框设置只读,后台可获取
    div 在同一行的 CSS处理
    在标签中添加属性
    (转)如何使用SignalTap II觀察reg與wire值? (SOC) (Verilog) (Quartus II) (SignalTap II)
    (转)如何使用ModelSim對Megafunction或LPM作仿真? (SOC) (MegaCore) (ModelSim)
    (笔记)TSL235新型光感器件强烈推荐使用
    (转)如何增加SignalTap II能觀察的reg與wire數量? (SOC) (Quartus II) (SignalTap II)
    (转) 如何將10進位轉2進位? (C/C++) (C)
    (转)如何使用ModelSim作前仿真與後仿真? (SOC) (Quartus II) (ModelSim)
    (笔记)关于LM3S片内FLASH编程的一点建议
  • 原文地址:https://www.cnblogs.com/lvyahui/p/4009943.html
Copyright © 2011-2022 走看看