Cod sursa(job #3378692)

Utilizator hugsfromluizaCroitoru Luiza hugsfromluiza Data 27 noiembrie 2026 16:36:21
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
const int INF = 1e9;
int main() {
    int n;
    fin >> n;
    vector<vector<int>> dist(n + 1, vector<int>(n + 1, INF));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            int x;
            fin >> x;
            if (i == j) {
                dist[i][j] = 0;
            } else if (x != 0) {
                dist[i][j] = x;
            }
        }
    }
    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) {
                    dist[i][j] = min(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) {
                fout << 0 << " ";
            } else {
                fout << dist[i][j] << " ";
            }
        }
        fout << "\n";
    }
    return 0;
}