• floyd裸码


     1 //floyd
     2 
     3 #include<cstdio>
     4 
     5 #include<iostream>
     6 
     7 #include<algorithm>
     8 
     9 using namespace std;
    10 
    11 int Maxn=999999;
    12 
    13 int a[110][110];
    14 
    15 int main(){
    16 
    17     int m,n,u,v,w;
    18 
    19     cin>>n>>m;
    20 
    21     for(int i=1;i<=m;i++){
    22 
    23         scanf("%d%d%d",&u,&v,&w);
    24 
    25         if(u!=v)
    26 
    27             a[u][v]=a[v][u]=w;//记录无向连通图
    28 
    29     }
    30 
    31     for(int i=1;i<=n;i++)
    32 
    33         for(int j=1;j<=n;j++){
    34 
    35             if(!a[i][j]&&i!=j)
    36 
    37                 a[i][j]=Maxn;//没有边,权值正无穷
    38 
    39             if(i==j)
    40 
    41                 a[i][j]=0;//到自己权值0
    42 
    43             
    44 
    45         }
    46 
    47     for(int k=1;k<=n;k++)
    48 
    49         for(int i=1;i<=n;i++)
    50 
    51             for(int j=1;j<=n;j++)
    52 
    53                 a[i][j]=min(a[i][j],a[i][k]+a[k][j]);//比较经过某点与直接连通的最小值
    54 
    55     for(int i=1;i<=n;i++){
    56 
    57         for(int j=1;j<n;j++)
    58 
    59             printf("%d ",a[i][j]);
    60 
    61         printf("%d
    ",a[i][n]);
    62 
    63     }
    64 
    65     return 0;

    }

  • 相关阅读:
    LeetCode 1245. Tree Diameter
    LeetCode 1152. Analyze User Website Visit Pattern
    LeetCode 1223. Dice Roll Simulation
    LeetCode 912. Sort an Array
    LeetCode 993. Cousins in Binary Tree
    LeetCode 1047. Remove All Adjacent Duplicates In String
    LeetCode 390. Elimination Game
    LeetCode 1209. Remove All Adjacent Duplicates in String II
    LeetCode 797. All Paths From Source to Target
    LeetCode 1029. Two City Scheduling
  • 原文地址:https://www.cnblogs.com/al76/p/8274627.html
Copyright © 2020-2023  润新知