Pagini recente » Cod sursa (job #2053120) | Cod sursa (job #1346184) | Cod sursa (job #862492) | Cod sursa (job #2000137) | Cod sursa (job #1700166)
#include <cstdio>
#include <iostream>
#include <algorithm>
#define MAX 100
void floyd_warshall( int graph[MAX][MAX], int dist[MAX][MAX], const int N ) {
for ( int i = 0; i < N; ++i ) {
for ( int j = 0; j < N; ++j ) {
dist[i][j] = graph[i][j];
}
}
for ( int k = 0; k < N; ++k ) {
for ( int i = 0; i < N; ++i ) {
for ( int j = 0; j < N; ++j ) {
dist[i][j] = std::min( dist[i][j], dist[i][k] + dist[k][j] );
}
}
}
}
int main(int argc, char const *argv[])
{
FILE *f_in, *f_out;
f_in = fopen ( "royfloyd.in", "r" );
f_out = fopen ( "royfloyd.out", "w" );
if ( !f_in ) {
perror ( "can't open file!");
return -1;
}
int N;
fscanf( f_in, "%d", &N);
int graph[MAX][MAX];
int dist[MAX][MAX];
for ( int i = 0; i < N; ++i ) {
for ( int j = 0; j < N; ++j ) {
fscanf(f_in, "%d", &graph[i][j]);
}
}
floyd_warshall( graph, dist, N );
for ( int i = 0; i < N; ++i ) {
for ( int j = 0; j < N; ++j ) {
fprintf(f_out, "%d ", dist[i][j] );
}
fprintf(f_out, "\n");
}
fclose( f_out );
fclose( f_in );
return 0;
}