Cod sursa(job #3361238)

Utilizator Zeno1789Zeno Ciuca Zeno1789 Data 22 iulie 2026 10:48:00
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <fstream>
#include <algorithm>
using namespace std;

ifstream cin ("royfloyd.in");
ofstream cout ("royfloyd.out");

const int INF=1e9;
int dist[105][105];

int main() {
    int n;
    cin>>n;
    for (int i=1; i<=n; ++i) {
        for (int j=1; j<=n; ++j) {
            cin>>dist[i][j];
            if (i!=j && 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 (dist[i][k]!=INF && dist[k][j]!=INF) {
                    if (dist[i][j]>dist[i][k]+dist[k][j]) {
                        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) {
                dist[i][j]=0;
            }
            cout<<dist[i][j]<<(j==n ? "" : " ");
        }
        cout<<"\n";
    }
}