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 }
  • 相关阅读:
    JS实现——用3L和5L量出4L的水
    岭回归与Lasso回归
    简单多元线性回归(梯度下降算法与矩阵法)
    【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法
    tensorflow sigmoid_cross_entropy_with_logits 函数解释
    tensorflow 线性回归解决 iris 2分类
    神经网络激活函数
    神经网络防止过拟合的方法
    网络流量分析——NPMD关注IT运维、识别宕机和运行不佳进行性能优化。智能化分析是关键-主动发现业务运行异常。科来做APT相关的安全分析
    cnn汉字识别 tensorflow demo
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4728520.html
Copyright © 2011-2022 走看看