Cod sursa(job #348323)

Utilizator serbanlupulupulescu serban serbanlupu Data 15 septembrie 2009 12:58:23
Problema Floyd-Warshall/Roy-Floyd Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
// infoarena
// pb: Floyd-Warshall/Roy-Floyd
// O(n^3)

#include <iostream>
#include <fstream>

using namespace std;

short n;
short a[101][101];

void read()
{
	fstream f("royfloyd.in", ios::in);
	f>>n;
	for (short i=1; i<=n; ++i)
		for (short j=1; j<=n; ++j)
			f>>a[i][j];
}

void solve()
{
	for (short i=1; i<=n; ++i)
		for (short j=1; j<=n; ++j)
			for (short k=1; k<=n; ++k)
				if (a[i][k] && a[k][j] && (a[i][j] > a[i][k] + a[k][j] || !a[i][j]) && i != j) a[i][j] = a[i][k] + a[k][j];
	fstream g("royfloyd.out", ios::out);
	for (short i=1; i<=n; ++i)
	{
		for (short j=1; j<=n; ++j)
			g<<a[i][j]<<" ";
		g<<"\n";
	}	
}

int main()
{
	read();
	solve();
	return 0;
}