Pagini recente » Cod sursa (job #1045719) | Cod sursa (job #823671)
Cod sursa(job #823671)
#include<cstdio>
#include<malloc.h>
#define infinite 0x3f3f3f3f
int main()
{
int n;
int **mat;
freopen("royfloyd.in", "r", stdin);
freopen("royfloyd.out", "w", stdout);
scanf("%d", &n);
mat = (int **) malloc(n * sizeof(int*));
for(int i = 0; i < n; i++)
mat[i] = (int*) malloc(n * sizeof(int));
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
scanf("%d", &mat[i][j]);
if(mat[i][j] == 0 && i != j)
mat[i][j] = infinite;
}
for(int k = 0; k < n; k++)
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(mat[i][k] + mat[k][j] < mat[i][j])
mat[i][j] = mat[i][k] + mat[k][j];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
if(mat[i][j] == infinite)
printf("0 ");
else
printf("%d ", mat[i][j]);
puts("");
}
return 0;
}