zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    诸葛亮会议
    软件工程第十次作业——例行报告
    Beta阶段中间产物
    Beta冲刺贡献分数分配结果
    “Hello World!”团队第六周的第六次会议
    “Hello World!”团队第六周的第五次会议
    Beta发布文案+美工
    “Hello World!团队”Beta发布—视频链接+文案+美工
    软件工程第九次作业——例行报告
    “Hello World!”团队第五周第五次会议
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/4978030.html
Copyright © 2011-2022 走看看