Cod sursa(job #143529)

Utilizator filipbFilip Cristian Buruiana filipb Data 26 februarie 2008 17:12:35
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 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)
			{
				if (D[i][k] == INF || D[k][j] == INF)
					continue;
				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;
}