zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    世界时钟国家中英文名称国家代码与北京的时差 一览
    拼写CAML查询的小工具
    The trust relationship between this workstation and the primary domain failed
    SharePoint 2003 架构介绍
    [经典文章翻译]垃圾收集: 在Microsoft .NET Framework中的自动化内存管理 第二部分
    [转] [精华] 跟我一起写 Makefile
    c#的常用排序
    MS SQL Server查询优化方法
    如何进行成功的创业程序员创业白皮书
    SQL编码规范
  • 原文地址:https://www.cnblogs.com/sdxk/p/4653692.html
Copyright © 2011-2022 走看看