zoukankan      html  css  js  c++  java
  • Poj1258--Agri-Net(Prime)

    Agri-Net
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 45330   Accepted: 18627

    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

    Source

     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 using namespace std;
     5 const int INF = 0x3f3f3f3f;
     6 int map[120][120], dis[120], vis[120];
     7 int n;
     8 void Prim()
     9 {
    10     memset(vis, 0, sizeof(vis));
    11     for(int i = 0; i < n; i++)
    12         dis[i] = map[0][i];
    13     vis[0] = 1; int sum = 0;
    14     for(int i = 1; i < n; i++)
    15     {
    16         int min = INF, temp;
    17         for(int j = 0; j < n; j++)
    18         {
    19             if(!vis[j] && dis[j] < min)
    20             {
    21                 temp = j;
    22                 min = dis[j];    
    23             } 
    24         } 
    25         vis[temp] = 1;
    26         sum += min;
    27         for(int j = 0; j < n; j++)
    28             if(!vis[j] && dis[j] > map[temp][j])
    29                 dis[j] = map[temp][j];
    30     }
    31     printf("%d
    ", sum);
    32 }
    33 int main()
    34 {
    35     while(~scanf("%d", &n))
    36     {
    37         for(int i = 0; i < n; i++)
    38             for(int j = 0; j < n; j++)
    39                 cin >> map[i][j];
    40         Prim();
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    JSON AND BSON
    xom报错 Exception in thread "main" java.net.UnknownHostException: file
    创建weblogic受管理服务器和安全文件
    创建weblogic domain
    安装weblogic步骤
    python读取excel表格生成sql语句 第一版
    nutz框架使用记录之Cnd.wrap
    IDEA 导入cordova3.5工程目录注意事项
    javascript类继承
    使用nodejs 访问mongodb
  • 原文地址:https://www.cnblogs.com/soTired/p/4731230.html
Copyright © 2011-2022 走看看