Cod sursa(job #1891237)

Utilizator GreeDGlavan George Florian GreeD Data 23 februarie 2017 20:38:00
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.67 kb
#include <stdlib.h>
#include <stdio.h>

#define INF 1001


int main(){
	FILE* f,*g;

	f = fopen("royfloyd.in","r");
	g = fopen("royfloyd.out","w");

	int n,i,j,k;

	fscanf(f,"%d",&n);

	int dist[100][100];

	for(i = 0; i < n; i++){
		for(j = 0; j < n; j++){
			fscanf(f,"%d",&dist[i][j]);
			if(dist[i][j] == 0)
				dist[i][j] = INF;
		}
	}

	for(k = 0; k < n; k++){
		for(i = 0; i < n; i++){
			for(j=0; j < n; j++){
				if(i!=j && dist[i][j] > dist[i][k] + dist[k][j])
					dist[i][j] = dist[i][k] + dist[k][j];
			}
		}
	}
	for(i = 0; i < n; i++){
		for(j = 0; j < n; j++){
			if(dist[i][j] == INF)
				dist[i][j] = 0;
			fprintf(g,"%d ",dist[i][j]);
		
		}
		fprintf(g,"\n");
	}

}