zoukankan      html  css  js  c++  java
  • USACO Section 3.1 AgriNet (agrinet)

    Agri-Net
    Russ Cox

    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.

    PROGRAM NAME: agrinet

    INPUT FORMAT

    Line 1: The number of farms, N (3 <= N <= 100).
    Line 2..end: The subsequent lines contain the N x N connectivity 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.

    SAMPLE INPUT (file agrinet.in)

    4
    0 4 9 21
    4 0 8 17
    9 8 0 16
    21 17 16 0
    

    OUTPUT FORMAT

    The single output contains the integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

    SAMPLE OUTPUT (file agrinet.out)

    28
    
    思路:一次prim,初学者写得丑请见谅。
    Executing...
       Test 1: TEST OK [0.000 secs, 3268 KB]
       Test 2: TEST OK [0.000 secs, 3268 KB]
       Test 3: TEST OK [0.000 secs, 3268 KB]
       Test 4: TEST OK [0.000 secs, 3268 KB]
       Test 5: TEST OK [0.000 secs, 3268 KB]
       Test 6: TEST OK [0.000 secs, 3268 KB]
       Test 7: TEST OK [0.000 secs, 3268 KB]
       Test 8: TEST OK [0.000 secs, 3268 KB]
       Test 9: TEST OK [0.000 secs, 3268 KB]
       Test 10: TEST OK [0.000 secs, 3268 KB]
    
    All tests OK.
     1 /*
     2 ID:wuhuaju2
     3 PROG:agrinet
     4 LANG:C++
     5 */
     6 
     7 #include <cstdio>
     8 #include <iostream>
     9 #include <cstdlib>
    10 #include <algorithm>
    11 #include <cstring>
    12 using namespace std;
    13 
    14 const int huge=2000000000;
    15 const int maxn=110;
    16 int a[maxn][maxn],d[maxn];
    17 int n,minn,node,ans;
    18 bool f[maxn];
    19 
    20 void close()
    21 {
    22     fclose(stdin);
    23     fclose(stdout);
    24     exit(0);
    25 }
    26 
    27 void work()
    28 {
    29 }
    30 
    31 void init ()
    32 {
    33 freopen("agrinet.in","r",stdin);
    34 freopen("agrinet.out","w",stdout);
    35 memset(d,0,sizeof(d));
    36 memset(a,0,sizeof(a));
    37 memset(f,false,sizeof(f));
    38 ans=0;
    39    scanf("%d",&n);
    40     for (int i=1;i<=n;i++)
    41     {
    42         for (int j=1;j<=n;j++)
    43             scanf("%d",&a[i][j]);
    44     }
    45     for (int i=2;i<=n;i++)
    46         d[i]=a[1][i];
    47     f[1]=true;
    48     for (int i=1;i<=n-1;i++)
    49     {
    50         minn=huge; //注意加边进去不能加入自己!
    51         for (int j=1;j<=n;j++)
    52         {
    53             if (not f[j] && d[j]<minn)
    54             {
    55                 minn=d[j];
    56                 node=j;
    57             }
    58         }
    59         ans+=minn;
    60         f[node]=true; //好像这句话可以保证了
    61     //    printf("node:%d minn:%d\n",node,minn);
    62         for (int j=1;j<=n;j++)
    63         {
    64             if (not f[j] && d[j]>a[node][j]) //枚举节点到该选择节点的距离
    65                 d[j]=a[node][j];
    66         }
    67         /*
    68         for (int j=1;j<=n;j++)
    69             printf("%d ",d[j]);
    70         printf("\n");
    71         */
    72     }
    73     printf("%d\n",ans);
    74 }
    75 
    76 int main ()
    77 {
    78     init();
    79     work();
    80     close();
    81     return 0;
    82 }
  • 相关阅读:
    ORACLE触发器详解
    论文笔记 Interpreting Black-Box Classifiers Using Instance-Level Visual Explanations
    Popush迭代2个人总结
    Popush迭代1个人总结
    Popush第5次会议记录
    Xv6代码阅读报告之进程调度
    Popush源代码学习报告
    Popush 第二次小组会议记录及分工
    Popush 用户故事
    “老衲印象”开发团队章程
  • 原文地址:https://www.cnblogs.com/cssystem/p/2899545.html
Copyright © 2011-2022 走看看