Cod sursa(job #1881600)

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

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

const int NMax = 103;
const int INF = 0x3f3f3f3f;

int n;
int a[NMax][NMax],b[NMax][NMax];

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

    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j){
            a[i][j] = INF;
            if(b[i][j] != 0){
                a[i][j] = b[i][j];
            }
        }
    }
    for(int inter = 1; inter <= n; ++inter){
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                if(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){
            if(a[i][j] == INF || i == j)
                g << 0 << ' ';
            else
                g << a[i][j] << ' ';
        }
        g << '\n';
    }
    return 0;
}