Cod sursa(job #3249830)

Utilizator adimiclaus15Miclaus Adrian Stefan adimiclaus15 Data 18 octombrie 2024 08:59:59
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.17 kb
#include <bits/stdc++.h>

using namespace std;

const int INF = 1e9;
const int NMAX = 100;

ifstream f("royfloyd.in");
ofstream g("royfloyd.out");

int dist[NMAX + 1][NMAX + 1];
//dist[i][j] = distanta de la nodul i la nodul j

int main() {
    int n;
    f >> n;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            f >> dist[i][j];
        }
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            if(dist[i][j] == 0 && i != j) {
                dist[i][j] = INF;
            }
        }
    }
    //dupa k pasi noi o sa avem drumul minim de la i la j
    //care trece prin primele k noduri
    for(int k = 1; k <= n; k++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                if(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] == INF) {
                g << 0 << ' ';
            } else {
                g << dist[i][j] << ' ';
            }
        }
        g << '\n';
    }
    return 0;
}