• Agri-Net


     
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 44373   Accepted: 18127

    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 #define INF 100000
     3 int n;
     4 int map[200][200];
     5 int dis[200],vis[200];
     6 int Prim(){
     7     for(int i=0;i<n;i++){
     8         vis[i]=0;
     9         dis[i]=INF;
    10     }
    11     dis[0]=0;
    12 
    13     for(int i=0;i<n;i++){
    14         int min=INF;
    15         int p;
    16         for(int j=0;j<n;j++){
    17             if(!vis[j]&&min>dis[j]){
    18                 min=dis[j];
    19                 p=j;
    20             }
    21         }
    22         vis[p]=1;
    23         for(int j=0;j<n;j++){
    24             if(!vis[j]&&dis[j]>map[p][j]){
    25                 dis[j]=map[p][j];
    26             }
    27         }
    28     }
    29     for(int i=1;i<n;i++){
    30         dis[0]+=dis[i];
    31     }
    32     return dis[0];
    33 }
    34 int main() {
    35     while(~scanf("%d",&n)){
    36     for(int i=0;i<n;i++){
    37         for(int j=0;j<n;j++){
    38             scanf("%d",&map[i][j]);
    39         }
    40     }
    41     int min=Prim();
    42     printf("%d
    ",min);
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    Java阶段测试题一
    HttpClient配置及运用(一)
    字符串数组及链表的应用:例题
    Java多线程
    String普通方法测试;可变参数
    Java连接mysql数据库
    JS练习
    foreach遍历、包装类、Object类
    Java多态总结
    类的关联,不同类属性的调用
  • 原文地址:https://www.cnblogs.com/sdxk/p/4653692.html
Copyright © 2020-2023  润新知