Cod sursa(job #2718863)

Utilizator dimi999Dimitriu Andrei dimi999 Data 9 martie 2021 12:00:19
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <bits/stdc++.h>
using namespace std;

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

int a[105][105], dist[105][105];

int main()
{
    int N;

    fin >> N;

    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= N; j++)
            fin >> a[i][j];

    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= N; j++)
            dist[i][j] = a[i][j];

    for(int aux = 1; aux <= N; aux++)
        for(int src = 1; src <= N; src++)
            for(int sink = 1; sink <= N; sink++)
            if(dist[src][aux] && dist[aux][sink] && src != sink)
                {
                    if(dist[src][sink] != 0)
                        dist[src][sink] = min(dist[src][sink], dist[src][aux] + dist[aux][sink]);
                    else
                        dist[src][sink] = dist[src][aux] + dist[aux][sink];
                }

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

    return 0;
}