Cod sursa(job #613149)

Utilizator MatahArdelean Selma Matah Data 16 septembrie 2011 23:27:20
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
//includes
#include <iostream>
#include <stdio.h>
using namespace std;

//defines
#define sizeMax 100

//global variables
int size;
int matrix[sizeMax][sizeMax];

//functions
void warshall()
{
        int k, i, j;
        for(k = 0; k < size; ++k)
          for(i = 0; i < size; ++i)
            for(j = 0; j < size; ++j)
            {
                if(matrix[i][k] && matrix[k][j] && (matrix[i][j] > matrix[i][k]+matrix[k][j] || matrix[i][j]) && i!=j)
                        matrix[i][j] = matrix[i][k] + matrix[k][j];
            }
}

int main()
{
	int i, j;
	//open files for read and write as stdin and stdout
	freopen("floyd.in", "r", stdin);
	freopen("floyd.out", "w", stdout);
	//read in size of the matrix
	scanf("%d", &size);
	//read in elements of the matrix
	for(i = 0; i < size; ++i)
	  for(j = 0; i < size; ++j)
		scanf("%d", &matrix[i][j]);
	//solve using the warshall algorithm
	warshall();
	
	for(i = 0; i < size; ++i)
	{
	   for(j = 0; j < size; ++j)
		printf("%d ", matrix[i][j]);
	   printf("\n");
	}

	return 0;
}