HDU 2544 - 最短路

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define LL long long
const int MAXN = 100 + 5;
const int INF = 0x3f3f3f3f;
 
int mp[MAXN][MAXN], n;
 
void Floyd()
{
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
}
 
int main()
{
    //freopen("input.txt", "r", stdin);
    int nr, a, b, c, i, j;
    while (scanf("%d%d", &n, &nr), n + nr)
    {
        for (i = 1; i <= n; i++)
            for (j = 1; j <= n; j++)
                mp[i][j] = (i == j ? 0 : INF);
        for (i = 0; i < nr; i++)
        {
            scanf("%d%d%d", &a, &b, &c);
            mp[a][b] = mp[b][a] = c;
        }
        Floyd();
        printf("%d\n", mp[1][n]);
    }
    return 0;
}

Powered by Jekyll and Theme by solid