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 }
  • 相关阅读:
    还是火柴排队(补一下归并排序的锅)
    火柴排队(NOIP2013)(附树状数组专题讲解(其实只是粗略。。。))
    转圈游戏(NOIP2013)
    接口和多态
    HttpClient-传入url得到json字符串( PostMethod method = new PostMethod(url)是个好方法)
    url和资源的再理解
    每天进步一点点- 资源与URI(吐血精华总结)
    每天进步一点点-一切皆对象/一次编写,到处运行/bean工厂
    java黑魔法-反射机制-02-通过Java反射调用其他类方法
    java黑魔法-反射机制-01
  • 原文地址:https://www.cnblogs.com/soTired/p/4731230.html
Copyright © 2011-2022 走看看