• 最小生成树 prime poj1287


    poj1287 裸最小生成树

    代码

     1 #include "map"
     2 #include "queue"
     3 #include "math.h"
     4 #include "stdio.h"
     5 #include "string.h"
     6 #include "iostream"
     7 #include "algorithm"
     8 #define abs(x) x > 0 ? x : -x
     9 #define max(a,b) a > b ? a : b
    10 #define min(a,b) a < b ? a : b
    11 
    12 using namespace std;
    13 
    14 int arc[55][55];
    15 
    16 void Prime(int n){
    17     int i,j,k,m;
    18     int sumweight = 0;
    19     int addnew[55];
    20     int lowcost[55];
    21     for(i=1; i<=n; i++){
    22         addnew[i] = 0;
    23         lowcost[i] = arc[1][i];
    24     }
    25     addnew[1] = 1;
    26     for(i=1; i<n; i++){
    27         k = 0,m = 999999;
    28         for(j=2; j<=n; j++){
    29             if(!addnew[j] && lowcost[j]<m){
    30                 m = lowcost[j];
    31                 k = j;
    32             }
    33         }
    34         sumweight += lowcost[k];
    35         addnew[k] = 1;
    36         for(j=2; j<=n; j++){
    37             if(!addnew[j] && arc[k][j]<lowcost[j])
    38                 lowcost[j] = arc[k][j];
    39         }
    40     }
    41     printf("%d
    ",sumweight);
    42 }
    43 
    44 int main(){
    45     int n,m,i,a,b,c;
    46     while(scanf("%d",&n)&&n){
    47         memset(arc,111,sizeof(arc));
    48         scanf("%d",&m);
    49         while(m--){
    50             scanf("%d%d%d",&a,&b,&c);
    51             if(arc[a][b]>c){
    52                 arc[a][b] = c;
    53                 arc[b][a] = c;
    54             }
    55         }
    56         Prime(n);
    57     }
    58     return 0;
    59 }
  • 相关阅读:
    Mybatis学习笔记14
    Mybatis学习笔记13
    Mybatis学习笔记12
    poj 2513
    poj 2001
    poj 1080
    poj 1703
    poj 1521
    poj 1384
    poj 1726
  • 原文地址:https://www.cnblogs.com/max88888888/p/6067922.html
Copyright © 2020-2023  润新知