Cod sursa(job #1881587)

Utilizator VladTiberiuMihailescu Vlad Tiberiu VladTiberiu Data 16 februarie 2017 16:39:50
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");

const int NMax = 103;

int n;
int a[NMax][NMax];

int main()
{
    f >> n;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            f >> a[i][j];
    for(int inter = 1; inter <= n; ++inter){
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                if(i != j){
                    if(a[i][j] != 0 && a[i][j] > a[i][inter] + a[inter][j]){
                        a[i][j] = a[i][inter] + a[inter][j];
                    }
                }
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            g << a[i][j] << ' ';
        }
        g << '\n';
    }
    return 0;
}