POJ - 1502 MPI Maelstrom

POJ - 1502 题目链接
最短路的最大路径长度问题
考虑到点数最大是100,而且输入方式是邻接矩阵的方式,直接上Floyed的,题目比较简单(就连我都能一次性过),直接上代码吧。

//Powered by Ck
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 110;
int ans[N][N], n;
void Floyed() {
    for(int k = 1; k <= n; k++)
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                if(ans[i][j] > ans[i][k] + ans[k][j])
                    ans[i][j] = ans[j][i] = ans[i][k] + ans[k][j];
    int sum = 0;
    for(int i = 1; i <= n; i++)
        if(ans[1][i] > sum)
            sum = ans[1][i];
    printf("%d\n", sum);
    // for(int i = 1; i <= n; i++)
    //     for(int j = 1; j <= n; j++)
    //         printf("%d%c", ans[i][j], j == n ? '\n' : ' ');
}
int main() {
    // freopen("in.txt", "r", stdin);
    string w;
    while(cin >> n) {
        for(int i = 2; i <= n; i++)
            for(int j = 1; j < i; j++) {
                cin >> w;
                if(w[0] == 'x') ans[i][j] = ans[j][i] = INF;
                else {
                    int l = w.size();
                    int temp = 0;
                    for(int k = 0; k < l; k++)
                        temp = temp * 10 + (w[k] - '0');
                    ans[i][j] = ans[j][i] = temp;
                }
            }
        // for(int i = 1; i <= n; i++)
        //     for(int j = 1; j <= n; j++)
        //         printf("%d%c", ans[i][j], j == n ? '\n' : ' ');
        Floyed();
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lifehappy/p/12619191.html
今日推荐