Cod sursa(job #2684138)

Utilizator raluca1234Tudor Raluca raluca1234 Data 12 decembrie 2020 23:16:01
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <iostream>
#include <fstream>

using namespace std;

const int N_MAX = 100;
const int INF = 1e6; 

int main() {
    ifstream cin("royfloyd.in");
    ofstream cout("royfloyd.out");
    
    int n;
    cin >> n;

    int d[N_MAX][N_MAX];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int cost;
            cin >> cost;
            d[i][j] = (cost == 0 && i != j) ? INF : cost;
        }
    }

    for (int k = 0; k < n; k++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (d[i][j] > d[i][k] + d[k][j]) {
                    d[i][j] = d[i][k] + d[k][j];
                }
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << d[i][j] << " ";
        }
        cout << '\n';
    }

    return 0;
}