Cod sursa(job #3266724)

Utilizator vladorovOroviceanu Vlad vladorov Data 10 ianuarie 2025 08:08:25
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <iostream>
#include <fstream>

#define INF 0x3F3F3F3F

using namespace std;

int n;

int adj[101][101], dist[101][101];

void roy_floyd(){
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            if(i==j){
                dist[i][j]=0;
            }else{
                dist[i][j]=adj[i][j];
            }
        }
    }

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

int main(){
    ifstream fin("royfloyd.in");
    ofstream fout("royfloyd.out");

    fin>>n;

    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            fin>>adj[i][j];
        }
    }

    roy_floyd();

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

    return 0;
}