Cod sursa(job #3251477)

Utilizator iulia_morariuIuli Morariu iulia_morariu Data 26 octombrie 2024 09:21:57
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <iostream>
#include <fstream>
#include <vector>
//#include <bits/stdc++.h>
#define in fin
#define out fout
#define oo 1000000

using namespace std;

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

int d[1005][1005];

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n; in >> n;

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            in >> d[i][j];
            if(d[i][j] == 0 && i != j) d[i][j] = oo;
        }
    }

    for(int k = 1; k <= n; k++){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                // e mai bine sa facem i->k + k->j decat i->j?
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++) out << d[i][j] << " ";
        out << '\n';
    }

    return 0;
}