zoukankan      html  css  js  c++  java
  • AcWing 854. Floyd求最短路 多源 邻接矩阵

    //不存在负权回路
    //边权可能为负数
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int N = 210, INF = 1e9;
    int n, m, Q;
    int d[N][N];//邻接矩阵
    void floyd() {//动态规划 
        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
    ", t);
        }
        return 0;
    }
  • 相关阅读:
    浅谈localStorage和sessionStorage的相关用法
    v-for中:key的作用总结
    textarea的placeholder无效问题解决
    6月10日
    6月9日
    6月8日
    6月7日
    6月6日
    10月5日
    6月4日
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11842177.html
Copyright © 2011-2022 走看看