Cod sursa(job #2040836)

Utilizator BogdanisarBurcea Bogdan Madalin Bogdanisar Data 16 octombrie 2017 16:53:14
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <algorithm>

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

#define ll long long
#define ull unsigned long long
#define pb push_back
const int NMax = 1e2 + 5;
const int inf = 1e9 + 5;

int N,M,nrChain;
int dist[NMax][NMax];

int main() {
    in>>N;
    for (int i=1;i <= N;++i) {
        for (int j=1;j <= N;++j) {
            int val;
            in>>val;
            dist[i][j] = (val != 0 || i == j) ? val : inf;
        }
    }

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

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