zoukankan      html  css  js  c++  java
  • Agri-Net

    Agri-Net

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
    Total Submission(s) : 8   Accepted Submission(s) : 4
    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<stdio.h>
     2 #include<string.h>
     3 const int INF=0x3f3f3f3f;
     4 const int MAXN=110;
     5 //#define MAX(x,y) (x>y?x:y) 
     6 int vis[MAXN],map[MAXN][MAXN],low[MAXN];
     7 int N,answer;
     8 void prime(){
     9     memset(vis,0,sizeof(vis));
    10     int flot=1,temp,k;
    11     answer=0;
    12     vis[1]=1;
    13     for(int i=1;i<=N;i++)low[i]=map[1][i];
    14     for(int i=1;i<=N;i++){
    15         temp=INF;
    16         for(int j=1;j<=N;j++)
    17         if(!vis[j]&&temp>low[j])temp=low[k=j];
    18         if(temp==INF){
    19         //    if(flot!=N)ans=0; 
    20         //    printf("flot=%d,N=%d
    ",flot,N);
    21             break;
    22         }
    23         answer+=temp;
    24         vis[k]=1;
    25         flot++;
    26         for(int j=1;j<=N;j++){
    27             if(!vis[j]&&low[j]>map[k][j])low[j]=map[k][j];
    28             }
    29         }
    30     }
    31 int main(){
    32                 int T,a;
    33                 while(~scanf("%d",&N)){
    34             memset(map,INF,sizeof(map));
    35                 for(int i=1;i<=N;i++){
    36                     for(int j=1;j<=N;j++){
    37                         scanf("%d",&a);
    38                         if(j>i){
    39                             if(a<map[i][j])map[i][j]=map[j][i]=a;
    40                         }
    41                     }
    42                 }
    43                 prime();
    44                 printf("%d
    ",answer);
    45                 }    
    46             return 0;
    47 }
  • 相关阅读:
    Storm集群环境搭建
    如何使用Python进行投资收益和风险分析
    [项目源码] JavaWeb校园宿舍管理系统
    (null) entry in command string: null chmod 0644
    Ubuntu设置代理服务器
    ZooKeeper异常:Error connecting service It is probably not running
    MySQL绿色版安装
    Pig关系型运算符例子
    pig分组统计例子
    java服务端项目开发规范
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4728520.html
Copyright © 2011-2022 走看看