AcWing 851. spfa seeking the shortest path

// absent negative cycles
 @ edge weights may be negative 
#include <CString> 
#include <the iostream> 
#include <algorithm>
 the using  namespace STD;
 const  int N = 210 , INF = 1E9;
 int n-, m, Q;
 int D [N] [N]; // adjacency matrix 
void Floyd () { // dynamic programming 
    for ( int K = . 1 ; K <= n-; K ++ )
         for ( int I = . 1 ; I <= n-; ++ i )
             for ( int j = 1; j <= n; j ++ )
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main() {
    scanf("%d%d%d", &n, &m, &Q);
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= n; j ++ )
            if (i == j) d[i][j] = 0;//处理自环 
            else d[i][j] = INF;
    while (m -- ) {
        int a, b, w;
        scanf("%d%d%d", &a, &b, &w);
        d[a][b] = min(d[a][b], w);
    }
    floyd();
    while (Q -- ) {
        int a, b;
        scanf("%d%d", &a, &b);
        int t = d[a][b];
        if (t > INF / 2) puts("impossible");
        else printf("%d\n", t);
    }
    return 0;
}

 

 

 

Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/11842168.html