Cod sursa(job #3333959)

Utilizator ThatScode24Mihai Focsa ThatScode24 Data 15 ianuarie 2026 17:18:55
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>
#include <vector>
#include <iostream>

#define INF 0x3f3f3f3f

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

void royFloyd(std::vector<std::vector<int>>&cost, int n) {
    for(int k=1;k<=n;++k) {
        for(int i=1;i<=n;++i) {
            for(int j=1;j<=n;++j) {
                if(cost[i][k] + cost[k][j] < cost[i][j]) {
                    cost[i][j] = cost[i][k] + cost[k][j];
                }
            }
        }
    }

    for(int i=1;i<=n;++i) {
        for(int j=1;j<=n;++j) {
            int x = (cost[i][j] == INF ? 0 : cost[i][j]);
            fout << x << " ";
        }
        fout << std::endl;
    }
}



int main(void) {
    int n, x;
    fin >> n;

    std::vector<std::vector<int>>cost(n+1, std::vector<int>(n+1, INF));

    for(int i=1;i<=n;++i) {
        for(int j=1;j<=n;++j) {
            fin >> x;
            cost[i][j] = x;
            std::cout<<x << " ";
        }
    }

    royFloyd(cost, n);

    return 0;
}