Cod sursa(job #2947881)

Utilizator Victor2006Nicola Victor-Teodor Victor2006 Data 26 noiembrie 2022 20:31:02
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <stdio.h>
#define N 100
#define INF 1000000000

long long cost[N][N];
int n;

int main() {
    FILE *fin, *fout;

    fin = fopen( "royfloyd.in", "r" );
    fscanf( fin, "%d", &n );
    for ( int i = 0; i < n; i ++ )
        for ( int j = 0; j < n; j ++ ) {
            fscanf( fin, "%lld", &cost[i][j] );
            if ( i != j && cost[i][j] == 0 )
                cost[i][j] = INF;
        }
    fclose( fin );

    for ( int k = 0; k < n; k ++ )
        for ( int i = 0; i < n; i ++ )
            for ( int j = 0; j < n; j ++ )
                if ( cost[i][k] + cost[k][j] < cost[i][j] )
                    cost[i][j] = cost[i][k] + cost[k][j];

    fout = fopen( "royfloyd.out", "w" );
    for ( int i = 0; i < n; i ++ ) {
        for ( int j = 0; j < n; j ++ ) {
            fprintf( fout, "%lld ", cost[i][j] );
        }
        fprintf( fout, "\n" );
    }
    fclose( fout );
    return 0;
}