Cod sursa(job #143516)

Utilizator filipbFilip Cristian Buruiana filipb Data 26 februarie 2008 17:05:50
Problema Floyd-Warshall/Roy-Floyd Scor Ascuns
Compilator cpp Status done
Runda Marime 0.71 kb
#include <stdio.h>
#include <assert.h>

#define NMax 128
#define FOR(i, a, b) for (i = a; i <= b; ++i) 
#define minim(a, b) ((a < b) ? a : b)
#define INF 2000000001

int N, D[NMax][NMax];

int main(void)
{
	int i, j, k;
	
	freopen("royfloyd.in", "r", stdin);
	freopen("royfloyd.out", "w", stdout);

	scanf("%d", &N);
	assert(1 <= N && N <= 100);
	FOR (i, 1, N)
		FOR (j, 1, N)
		{
			scanf("%d", &D[i][j]);
			assert(0 <= D[i][j] && D[i][j] <= 1000);
			if (!D[i][j] && i != j)
				D[i][j] = INF;
		}
	
	FOR (k, 1, N)
		FOR (i, 1, N)
			FOR (j, 1, N)
				D[i][j] = minim(D[i][j], D[i][k] + D[k][j]);	
	
	FOR (i, 1, N)
	{
		FOR (j, 1, N)			
			printf("%d ", (D[i][j] == INF) ? (0) : (D[i][j]));
		printf("\n");
	}
	
	return 0;
}