zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    转: 分布式系统编程,你到哪一级了?
    window屏幕朝向的调整 Alt + Ctrl + 上下左右箭头
    win10的安装与下载
    Zookeeper的学习材料
    配置文件的格式选型
    转: YAML 语言教程 from(阮一峰)
    Eclipse的 JSON Edit插件
    转: 如何为你的开源项目选择一个合适的开源协议?
    在Eclipse中使用SVN插件subclipse的教程
    我们在呼唤上帝还是在召唤恶魔——警惕人工智能
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3379003.html
Copyright © 2011-2022 走看看