其他分享
首页 > 其他分享> > acwing 854 Floyd 求最短路

acwing 854 Floyd 求最短路

作者:互联网

 三重循环

#include<iostream>
#include<algorithm>

using namespace std ;

const int N = 209 ,  INF = 1e9 ;
int n , m , k; 
int dist[N][N] ;

void foyld()
{
    for(int k = 1 ; k<=n ; k++)
    {
        for(int i = 1 ; i <= n ; i++)
        {
            for(int j= 1 ; j <= n ; j++)
            {
                dist[i][j] =min(dist[i][j] , dist[i][k] + dist[k][j])  ;
            }
        }
    }
}

int main()
{
    cin >> n >> m >> k ;
    for(int i = 1 ; i <= n ; i++)
    {
        for(int j = 1 ; j<= n ; j++)
        {
            if(i == j) dist[i][j] = 0 ;
            else dist[i][j] = INF ;
         }
    }
    for(int i = 1 ; i <= m ; i++)
    {
        int x , y , z ;
        cin >> x >> y >> z ;
        dist[x][y] = min(dist[x][y] , z) ;
    }
    foyld() ;
    while(k--)
    {
        int a,b;
        cin>>a>>b;
        int t=dist[a][b];
        if(t>INF/2)puts("impossible");
        else 
        cout<<dist[a][b]<<endl;
    }
    return 0 ;
}

标签:854,dist,cout,int,foyld,Floyd,INF,include,acwing
来源: https://blog.csdn.net/weixin_51658930/article/details/122842737