Cod sursa(job #1967855)

Utilizator MaligMamaliga cu smantana Malig Data 17 aprilie 2017 10:51:04
Problema Floyd-Warshall/Roy-Floyd Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <algorithm>

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

typedef long long ll;
const int NMax = 1e3 + 5;
const int inf = 1e9 + 5;

int N;
int dist[NMax][NMax];

int main() {
    in>>N;
    for (int i=1;i<=N;++i) {
        for (int j=1;j<=N;++j) {
            in>>dist[i][j];
            if (i!=j && !dist[i][j]) {
                dist[i][j] = inf;
            }
        }
    }

    for (int k=1;k<=N;++k) {
        for (int i=1;i<=N;++i) {
            for (int j=1;j<=N;++j) {
                if (dist[i][j] > dist[i][k] + dist[k][j]) {
                    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] == inf) ? 0 : dist[i][j])<<' ';
        }
        out<<'\n';
    }

    in.close();out.close();
    return 0;
}