Алгоритм Флойда
#include <iostream>
using namespace std;
 
#define MAX 101
 
int n;
int W[MAX][MAX];
int MAXINT = 1000000000;
 
void Floyd()
{
    int through_k;
    for (int k = 0; k < n; k++)
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                through_k = W[i][k] + W[k][j];
                if (through_k < W[i][j])
                {
                    W[i][j] = through_k;
                }
            }
        }
    }
}
 
int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
 
    cin >> n;
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int a = 0;
            cin >> a;
            if (a == -1) a = MAXINT;
            W[i][j] = a;
        }
    }
 
    Floyd();
 
    int max = 0;
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (W[i][j] == MAXINT) W[i][j] = 0;
        }
    }
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (W[i][j] > max)
            {
                max = W[i][j];
            }
        }
    }
 
    cout << max << endl;
 
    return 0;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License