Cod sursa(job #775653)

Utilizator alex_unixPetenchea Alexandru alex_unix Data 8 august 2012 17:18:44
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb

#include <cstdio>

const unsigned short MAX_VERTICES(100);
const unsigned short MAX_PATH(1000);
const signed int LONGEST_POSSIBLE_PATH(MAX_PATH * MAX_VERTICES);

signed int graph_matrix [MAX_VERTICES] [MAX_VERTICES];

void FloydWarshall (unsigned short n)
{
	unsigned short from, intermediate, to;
	signed int path, new_path;
	for (intermediate = 0 ; intermediate < n ; ++intermediate)
		for (from = 0 ; from < n ; ++from)
		{
			path = graph_matrix[from][intermediate];
			if (path == LONGEST_POSSIBLE_PATH)
				continue;
			for (to = 0 ; to < n ; ++to)
			{
				new_path = graph_matrix[intermediate][to];
				if (new_path == LONGEST_POSSIBLE_PATH || from == to)
					continue;
				new_path += path;
				if (new_path < graph_matrix[from][to])
					graph_matrix[from][to] = new_path;
			}
		}
}

int main (void)
{
	unsigned short n;
	std::freopen("royfloyd.in","r",stdin);
	std::scanf("%hu",&n);
	unsigned short row, collum;
	signed int number;
	for (row = 0 ; row < n ; ++row)
		for (collum = 0 ; collum < n ; ++collum)
		{
			std::scanf("%d",&number);
			if (!number)
			    number = LONGEST_POSSIBLE_PATH;
			graph_matrix[row][collum] = number;
		}
	std::fclose(stdin);
	row = 0;
	FloydWarshall(n);
	std::freopen("royfloyd.out","w",stdout);
	for (row = 0 ; row < n ; ++row)
	{
		for (collum = 0 ; collum < n ; ++collum)
		{
			number = graph_matrix[row][collum];
			if (!number || number == LONGEST_POSSIBLE_PATH)
				std::printf("0 ");
			else
				std::printf("%d ",number);
		}
		std::printf("\n");
	}
	std::fclose(stdout);
	return 0;
}