HDOJ-1874 畅通工程续(Floyed)

多源最短路径问题

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <climits>

using namespace std;
#define maxn 205
int Graph[maxn][maxn];

int main()
{
    //freopen("in.txt", "r", stdin);
    int n, m;

    while(cin >> n >> m)
    {
        memset(Graph, -1, sizeof(Graph));
        for(int i = 0; i < m; i++)
        {
            int a, b, c;

            scanf("%d%d%d", &a, &b, &c); 
            if(Graph[a][b] < 0 || Graph[a][b] > c)
            Graph[a][b] = Graph[b][a] = c;
        }
        for(int i = 0; i < n; i++)
          Graph[i][i] = 0;
        for(int i = 0; i < n; i++)
         for(int j = 0; j < n; j++)
         for(int h = 0; h < n; h++)
         {

             if(Graph[j][i] > 0 && Graph[i][h] > 0 && (Graph[j][h] > Graph[j][i] + Graph[i][h] 
             || Graph[j][h] < 0))
             {
                 Graph[j][h] = Graph[j][i] + Graph[i][h];
             }
         }
         int a, b;

         scanf("%d%d", &a, &b);
         cout << Graph[a][b] << endl;
    }
    return 0;
}

你可能感兴趣的:(HDOJ-1874 畅通工程续(Floyed))