Cod sursa(job #3271503)

Utilizator BogdancxTrifan Bogdan Bogdancx Data 26 ianuarie 2025 13:26:00
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    ifstream fin("royfloyd.in");
    ofstream fout("royfloyd.out");
    
    int n;
    
    fin >> n;
    
    vector<vector<int>> graph(n + 1, vector<int>(n + 1, 1e9));
    
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            fin >> graph[i][j];
        }
        graph[i][i] = 0;
    }
    
    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(graph[i][j] > graph[i][k] + graph[k][j]) {
                    graph[i][j] = graph[i][k] + graph[k][j];
                }
            }
        }
    }
    
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(graph[i][j] == 1e9) fout << 0 << ' ';
            else fout << graph[i][j] << ' ';
        }
        fout << endl;
    }
    
    return 0;
}