zoukankan      html  css  js  c++  java
  • Floyd-Warshall

    Description

    
    

    第一行四个数为n,m,n表示顶点个数,m表示边的条数。

    接下来m行,每一行有三个数t1、t2 和t3,表示顶点t1到顶点t2的路程是t3。请注意这些t1->t2是单向的。

    
    

    Output

    
    
    输出一个n*n的矩阵,第n行第n列表示定点n到n的距离。每一行两个数间由空格隔开
    
    

    Sample Input

    
    
    5 8
    1 2 2
    2 3 3
    3 4 4
    4 5 5
    5 3 3
    3 1 4
    2 5 7
    1 5 10
    
    
    

    Sample Output

    
    
    0 2 5 9 9
    7 0 3 7 7
    4 6 0 4 9
    12 14 8 0 5
    7 9 3 7 0
    
    
    

    More Info

    
    
    输出结果每行的最后一个数字后不需要留空格哦~



     1 #include<iostream>
     2 //#include<fstream>
     3 #define inf 0x3f3f3f3f
     4 using namespace std;
     5 int m,n;
     6 int e[1002][1002];
     7 int main(){
     8 //    fstream file("haha.txt");
     9 //    file>>n>>m;
    10     cin>>n>>m; 
    11     for(int i=1;i<=n;i++){//初始化 
    12         for(int j=1;j<=n;j++){
    13             if(i==j)
    14                 e[i][j]=0;
    15             else
    16                 e[i][j]=inf;    
    17         }
    18     }
    19     for(int i=1;i<=m;i++){//录入 
    20         int a,b,c;
    21         cin>>a>>b>>c;
    22         e[a][b]=c;
    23     }
    24     for(int k=1;k<=n;k++){//弗洛伊德核心算法 
    25         for(int i=1;i<=n;i++){
    26             for(int j=1;j<=n;j++){
    27                 if(e[i][j]>e[i][k]+e[k][j])
    28                     e[i][j]=e[i][k]+e[k][j];
    29             }
    30         }
    31     }
    32     for(int i=1;i<=n;i++){//输出 
    33         for(int j=1;j<=n;j++){
    34             cout<<e[i][j];
    35             if(j!=n)
    36             cout<<" ";
    37         }
    38         cout<<endl;
    39     }
    40     return 0;
    41 }




     
  • 相关阅读:
    苹果
    对称排序
    车牌号
    比较字母大小
    队花的烦恼一
    VF
    荷兰国旗问题
    字符串逆序输出
    Python多进程库multiprocessing创建进程以及进程池Pool类的使用
    Redis提供的持久化机制
  • 原文地址:https://www.cnblogs.com/fate-/p/12242464.html
Copyright © 2011-2022 走看看