Cod sursa(job #3251482)

Utilizator Xutzu358Ignat Alex Xutzu358 Data 26 octombrie 2024 09:28:57
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");
int inf = 1000000000;
int n; /// n - nr noduri
int dist[1005][1005]; /// matrice cu distante

int main() {
    f >> n;
    for (int i=1;i<=n;i++) {
        for (int j=1;j<=n;j++) {
            f >> dist[i][j];
            if (dist[i][j] == 0)
                dist[i][j] = inf;
            /// dist[i][j] = inf => nu exista muchie intre i si j
            /// dist[i][j] = 0 <=> i == j (depinde de la caz la caz)
            /// dist[i][j] != 0 => exista muchie intre i si j
        }
    }

    for (int k=1;k<=n;k++) {
        for (int i=1;i<=n;i++) {
            for (int j=1;j<=n;j++) {
                /// incercam sa actualizam dist (i,j) trecand prin nodul k
                if (i != j)
                    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) g << 0 << " ";
            else g << dist[i][j] << " ";
        }
        g << '\n';
    }
    return 0;
}