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

  • 相关阅读:
    margin问题
    IE6里面子集尺寸大的会把父亲撑大
    第一个元素<flout>写了,想在他的旁边加一个元素.IE6会出现缝隙. 不要用margin撑开,要用flout
    兼容性,float
    HTML5的兼容问题以及调用js文件的方法
    表单
    表格的编写,课程表
    SmartThreadPool
    C# 多线程的等待所有线程结束的一个问题
    DataTable保存与读取 stream
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3379003.html
Copyright © 2011-2022 走看看