Cod sursa(job #1448231)

Utilizator Andrei1998Andrei Constantinescu Andrei1998 Data 6 iunie 2015 14:42:04
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>

#define inf 2000005
using namespace std;

int dist[105][105];

int main()
{
    ifstream cin("royfloyd.in");
    ofstream cout("royfloyd.out");

    int n = 0;
    cin >> n;

    int j;
    for (int i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            cin >> dist[i][j];

    for (int i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            if (!dist[i][j])
                dist[i][j] = inf;

    int i, k;
    for (k = 1; k <= n; k++)
        for (i = 1; i <= n; i++)
            for (j = 1; j <= n; j++)
                if (i != j && dist[i][k] + dist[k][j] < dist[i][j])
                    dist[i][j] = dist[i][k] + dist[k][j];

    for (i = 1; i <= n; i++)
        dist[i][i] = 0;

    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            cout << dist[i][j] << " \n"[j == n];

    cin.close();
    cout.close();
    return 0;
}