Cod sursa(job #3248213)

Utilizator db_123Balaban David db_123 Data 11 octombrie 2024 05:51:33
Problema Floyd-Warshall/Roy-Floyd Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <fstream>
#include <vector>


using namespace std;

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

struct Info {
    int node2;
    int w;
    Info() = default;
    Info(int node2, int w) : node2(node2), w(w) {}
};

int n;
vector<vector<int>> graph;

void read() {
    cin >> n;
    graph.resize(n + 1, vector<int>(n + 1));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> graph[i][j];
        }
    }
}

void solve() {

    vector<vector<int>> dist(n + 1, vector<int>(n + 1, 1e9));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (graph[i][j] == 0) {
                continue;
            }
            dist[i][j] = min(dist[i][j], graph[i][j]);
        }
    }

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

int main() {

    read();
    solve();
    return 0;
}