Cod sursa(job #3215943)

Utilizator lolismekAlex Jerpelea lolismek Data 15 martie 2024 14:47:49
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>

#define pii pair <int, int>
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define pb push_back
#define fr first
#define sc second
#define vt vector
#define ub upper_bound
#define lb lower_bound

using namespace std;

string filename = "royfloyd";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");

const int NMAX = 100;
const int INF = 1e9;

int r[NMAX + 1][NMAX + 1];


int main(){

    int n;
    fin >> n;

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            fin >> r[i][j];

            if(r[i][j] == 0){
                r[i][j] = +INF;
            }
        }
    }

    for(int k = 1; k <= n; k++){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                r[i][j] = min(r[i][k] + r[k][j], r[i][j]);
            }
        }
    }

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(i == j){
                fout << 0 << ' ';
                continue;
            }
            fout << r[i][j] << ' ';
        }
        fout << '\n';
    }

    return 0;
}