Cod sursa(job #3177094)

Utilizator Barbu_MateiBarbu Matei Barbu_Matei Data 28 noiembrie 2023 15:32:47
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>
using namespace std;

int n, m;
int dist[101][101];

void royfloyd() {
    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][k] && dist[k][j] && (dist[i][k] + dist[k][j] < dist[i][j] || dist[i][j] == 0)) {
                    dist[i][j] = dist[i][k] + dist[k][j];
                }
            }
        }
    }
}

int main() {
    ifstream cin("roy-floyd.in");
    ofstream cout("roy-floyd.out");
    cin >> n >> m;
    for (int i = 1; i <= m; ++i) {
        int x, y, c;
        cin >> x >> y >> c;
        dist[x][y] = c;
    }
    royfloyd();
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (dist[i][j] == 0 && i != j) {
                cout << -1;
            } else {
                cout << dist[i][j];
            }
            cout << " ";
        }
        cout << "\n";
    }
}