Cod sursa(job #3325351)

Utilizator ioana_mitreaMitrea Ioana ioana_mitrea Data 25 noiembrie 2025 12:52:44
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>
using namespace std;

const int INF = 999999999;

int main(){
    ifstream reader("royfloyd.in");
    ofstream writer("royfloyd.out");
    int n;
    reader>>n;
    int dist[101][101];

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

    for(int k=0; k<n; k++)
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                if(dist[i][k]<INF && dist[k][j]<INF)
                    if(dist[i][j] > dist[i][k] + dist[k][j])
                        dist[i][j] = dist[i][k] + dist[k][j];

    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(dist[i][j]==INF) 
                writer<<0;
            else
                writer<<dist[i][j]<<" ";
        }
        writer<<endl;
    }

    return 0;
}