Cod sursa(job #3265723)

Utilizator MrPuzzleDespa Fabian Stefan MrPuzzle Data 2 ianuarie 2025 18:39:47
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.94 kb
#include <bits/stdc++.h>

#define INF 99999999

#define DIM 1000

using namespace std;

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

//ifstream f("filesmodel.in");
//ofstream g("filesmodel.out");

int n;
int a[DIM+5][DIM+5];

signed main(){

    f>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            f>>a[i][j];
            if(a[i][j] == 0 && i!=j){
                a[i][j] = INF;
            }
        }
    }

    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(a[i][j] > a[i][k] + a[k][j]){
                    a[i][j] = a[i][k] + a[k][j];
                }
            }
        }
    }

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(a[i][j] == INF){
                g<<"0 ";
            }else{
                g<<a[i][j]<<" ";
            }
        }
        g<<'\n';
    }
    return 0;
}