Cod sursa(job #3202242)

Utilizator theo234Badea Razvan Theodor theo234 Data 11 februarie 2024 10:57:43
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>
using namespace std;

const int INF = 1000;

ifstream cin("royfloyd.in");
ofstream cout("royfloyd.out");

void Floyd(int a[][100], int n){
    int dist[n][n];
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            dist[i][j] = a[i][j];

    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 && dist[i][k] + dist[k][j] < dist[i][j])
                    dist[i][j] = dist[i][k] + dist[k][j];

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++)
            cout << dist[i][j] << " ";
        cout << '\n';
    }

}

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

    Floyd(a, n);
    return 0;
}