Cod sursa(job #3251490)

Utilizator abelesefBurduhos Abel abelesef Data 26 octombrie 2024 09:34:11
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
int n,dist[1005][1005];
const int inf = 1000000000;
int main() {
        fin>>n;
        for (int i = 1;i<=n;++i) {
                for (int j = 1;j<=n;++j) {
                        fin>>dist[i][j];
                        if (dist[i][j]==0)
                                dist[i][j] = inf;
                }
        }
        for (int k = 1;k<=n;++k) {
                for (int i = 1;i<=n;++i) {
                        for (int j = 1;j<=n;++j) {
                                if (i!=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]==inf) {
                                fout<<0<<" ";
                        } else {
                                fout<<dist[i][j]<<" ";
                        }
                }
                fout<<'\n';
        }
        fout.close();
        fin.close();
        return 0;
}