Cod sursa(job #2907279)

Utilizator Apyxabcdefghij Apyx Data 29 mai 2022 15:13:20
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <bits/stdc++.h>
#define nmax 100

using namespace std;

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

long long dist[nmax+5][nmax+5];

int main() {
    int n; fin >> n;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            dist[i][j] = INT_MAX;
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            fin >> dist[i][j];
        }
    }
    for(int i = 1; i <= n; i++) {
        dist[i][i] = 0;
    }
    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(1LL*dist[i][j], 1LL*dist[i][k] + dist[k][j]);
                }
            }
        }
    }
    for(int i = 1; i <= n; i++, fout << "\n") {
        for(int j = 1; j <= n; j++) {
            if(dist[i][j] == INT_MAX) {
                fout << 0 << " ";
            } else {
                fout << dist[i][j] << " ";
            }
        }
    }
    return 0;
}