Cod sursa(job #3038622)

Utilizator begusMihnea begus Data 27 martie 2023 16:29:24
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <bits/stdc++.h>
using namespace std;

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

const int NMAX=101;

int n, dist[NMAX][NMAX];
vector<pair<int, int>> adj[NMAX];

int main(){
    fin >> n;
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            int w;
            fin >> w;
            if(w==0 && i!=j) dist[i][j]=INT_MAX/2;
            if(w){
                adj[i].push_back({j, w});
                dist[i][j]=w;
            } 
        }
    }
    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++){
            if(dist[i][j]==INT_MAX/2) fout << 0 << ' ';
            else fout << dist[i][j] << ' ';
        }
        fout << '\n';
    }
}