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 }
  • 相关阅读:
    Win32 程序开发入门:一个最简单的Win32程序
    DirectShow 进行视频预览和录制
    DirectShow 获取音视频输入设备列表
    (原)关于人民币找零钱的问题
    (转)The C10K problem翻译
    (原)kenel开机logo的制作
    (原)关于udp的socket发送数据耗时的问题探讨
    (转)x264的一些参数设置对编码效率的影响
    (原)关于获取ffmpeg解析rtsp流sdp中带有sps,pps的情况
    (转)java 层调用Jni(Ndk) 持久化c c++ 对象
  • 原文地址:https://www.cnblogs.com/sdxk/p/4653692.html
Copyright © 2011-2022 走看看