Cod sursa(job #3377748)

Utilizator matei__bBenchea Matei matei__b Data 24 noiembrie 2026 10:42:11
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <bits/stdc++.h>
using namespace std;

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

const int NMAX=1e2+8;

int n;
long long dist[NMAX][NMAX];

int main() {
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);

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

    for(int k=1; k<=n; k++) {
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=n; j++) {
                if(dist[i][k]==0 || dist[k][j]==0) {
                    continue;
                }

                dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
            }
        }
    }

    for(int i=1; i<=n; i++,fout << "\n") {
        for(int j=1; j<=n; j++) {
            if(i==j) {
                fout << 0 << " ";
            }
            else {
                fout << dist[i][j] << " ";
            }
        }
    }

    return 0;
}