Cod sursa(job #2947876)

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

int cost[N][N], m[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, "%d", &cost[i][j] );
        }
    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, "%d ", cost[i][j] );
        }
        fprintf( fout, "\n" );
    }
    fclose( fout );
    return 0;
}