Cod sursa(job #2233644)

Utilizator TudoseSanzianaTudose Sanziana TudoseSanziana Data 23 august 2018 19:34:05
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>
using namespace std;

ifstream in("royfloyd.in");
ofstream out("royfloyd.out");

const int N_MAX = 100;

int n;
int dist[N_MAX + 2][N_MAX + 2];

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

    for(int k = 1; k <= n; k++)
    {
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
            {
                if(i == j)
                    continue;

                if(dist[i][k] && dist[k][j])
                {
                    if(dist[i][j] == 0)
                        dist[i][j] = dist[i][k] + dist[k][j];
                    else
                        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
                }
            }
    }

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
                out << dist[i][j] << ' ';
        out << '\n';
    }

    return 0;
}