• Agri-Net prime算法


    Problem 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
    **************************************************************************************************************************
    最小生成树
    **************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<queue>
     7 using namespace std;
     8 int map[2010][2010];
     9 int vis[2010],n;
    10 int prime()
    11 {
    12     int lowcost[2010],lowset[2010];
    13     int it,jt,kt,sum=0;
    14     memset(vis,0,sizeof(vis));
    15     for(it=1;it<=n;it++)//初始化
    16     {
    17         lowcost[it]=map[1][it];
    18         //cout<<"lowc::"<<lowcost[it]<<endl;
    19     }
    20     vis[1]=1;
    21     for(it=1;it<n;it++)
    22     {
    23         jt=1;
    24         while(vis[jt])jt++;
    25         for(kt=1;kt<=n;kt++)//找到最短边
    26         {
    27             if(!vis[kt]&&lowcost[kt]<lowcost[jt])jt=kt;
    28         }
    29         //cout<<"j:: "<<jt<<endl;
    30         sum+=lowcost[jt];
    31         //cout<<"lowcost[jt]:: "<<lowcost[jt]<<endl;
    32         //cout<<"sum: "<<sum<<endl;
    33         vis[jt]=1;
    34         for(kt=1;kt<=n;kt++)//更新最短边
    35         {
    36             if(lowcost[kt]>map[jt][kt])
    37             {
    38                 lowcost[kt]=map[jt][kt];
    39                 //lowset[k]=j;
    40                 //cout<<"lowset: "<<lowset[kt]<<endl;
    41             }
    42         }
    43 
    44     }
    45     return sum;
    46 }
    47 int main()
    48 {
    49     int i,j,k;
    50     while(scanf("%d",&n)!=EOF)
    51     {
    52         for(i=1;i<=n;i++)
    53          for(j=1;j<=n;j++)
    54          {
    55             scanf("%d",&map[i][j]);
    56             //cout<<"map:: "<<map[i][j]<<endl;
    57          }
    58          printf("%d
    ",prime());
    59     }
    60     return 0;
    61 }
    View Code

  • 相关阅读:
    【2019年8月版本】OCP 071认证考试最新版本的考试原题-第4题
    【2019年8月版本】OCP 071认证考试最新版本的考试原题-第3题
    【2019年8月版本】OCP 071认证考试最新版本的考试原题-第2题
    【2019年8月版本】OCP 071认证考试最新版本的考试原题-第1题
    二分匹配题集
    004 IOC---IOC容器
    003 简单使用spring
    002 IOC--Bean创建的分析
    001 spring介绍
    012 shiro的jsp标签
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3379003.html
Copyright © 2020-2023  润新知