• POJ1258Agri-Net(prime基础)


    Agri-Net
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 46811   Accepted: 19335

    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
    题意:输入n个数,接着是n个数之间距离形成的n*n的矩阵,求最小生成树。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <queue>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 const int MAX = 100 + 5;
     9 const int INF = 10000000;
    10 int g[MAX][MAX],n,dist[MAX],vis[MAX];
    11 int main()
    12 {
    13     while(scanf("%d", &n) != EOF)
    14     {
    15         for(int i = 1; i <= n; i++)
    16         {
    17             for(int j = 1; j <= n; j++)
    18             {
    19                 scanf("%d", &g[i][j]);
    20             }
    21         }
    22         memset(dist,0,sizeof(dist));
    23         memset(vis,false,sizeof(vis));
    24         for(int i = 1; i <= n; i++)
    25             dist[i] = g[1][i];
    26 
    27         vis[1] = true;
    28         int sum = 0;
    29         for(int i = 1; i < n; i++)
    30         {
    31             int pos,minn = INF;
    32             for(int j = 1; j <= n; j++)
    33             {
    34                 if(vis[j] == false)
    35                 {
    36                     if(dist[j] < minn)
    37                     {
    38                         pos = j;
    39                         minn = dist[j];
    40                     }
    41                 }
    42             }
    43             vis[pos] = true;
    44             sum += minn;
    45             for(int j = 1; j <= n; j++)
    46                 dist[j] = min(dist[j],g[pos][j]);
    47         }
    48         printf("%d
    ",sum);
    49     }
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    动态规划算法介绍——概念、意义及应用、例题
    两个大数相减
    删除apache的签名的shell脚本
    C++中智能指针的设计和使用
    eclipse save action不起作用
    [leetcode]51. N-QueensN皇后
    [leetcode]33. Search in Rotated Sorted Array旋转过有序数组里找目标值
    [leetcode]88. Merge Sorted Array归并有序数组
    [leetcode]636. Exclusive Time of Functions函数独占时间
    [leetcode]257. Binary Tree Paths二叉树路径
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/4978030.html
Copyright © 2020-2023  润新知