HDU - 1874 - 畅通工程续(Floyd)

题意:给你节点数和边的条数,再给你一个出发点和结束点,让你求最短路径

/************************************************************************* > Author: violet0908 > Mail: [email protected] > Created Time: 2015年07月22日 星期三 ************************************************************************/

#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 2005
#define min(a, b) (a)<(b)?(a):(b)
#define INF 0xffffff
using namespace std;

int map[MAXN][MAXN];
int n, m;

void Floyd(){
    for(int k = 0; k < n; k++)
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++){
                map[i][j] = min(map[i][j], map[i][k]+map[k][j]);
            }
}

int main() {
    int i, j;
    while(scanf("%d%d", &n, &m)!=EOF){
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                map[i][j] = INF;

        for(int i = 0; i < m; i++){
            int a, b, c;
            scanf("%d %d %d", &a, &b, &c);
            if(map[a][b] > c)
                map[a][b] = map[b][a] = c;
        }

        Floyd();

        int a, b;
        scanf("%d %d", &a, &b);
        if(a == b)
            printf("0\n");
        else if(map[a][b] == INF)
            printf("-1\n");
        else
            printf("%d\n", map[a][b]);
    }
    return 0;
}

你可能感兴趣的:(最短路径,杭电,floyd)