Cod sursa(job #1513474)

Utilizator Andrei66Andrei Rusu Andrei66 Data 29 octombrie 2015 16:33:54
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <bits/stdc++.h>

#define x first
#define y second
#define VM 105
#define INF 1001
#define pb push_back
#define ppb pop_back
#define ll long long
#define pii pair<int, int>

using namespace std;

//don't forget to put input in the file!!!
ifstream f("royfloyd.in");
ofstream g("royfloyd.out");

int n, mat[VM][VM], pre[VM][VM], l[VM][VM];

int main(){ 
	
	f>>n;
	for(int i=1; i<=n; ++i)
		for(int j=1; j<=n; ++j){
			f>>mat[i][j];
			if(i != j)
				if(mat[i][j] == 0)
					l[i][j] = INF;
				else l[i][j] = mat[i][j];
			else l[i][j] = 0;
		}

	for(int k=1; k<=n; ++k){
		for(int i=1; i<=n; ++i)
			for(int j=1; j<=n; ++j){
				if(l[i][j] > l[i][k] + l[k][j]){
					l[i][j] = l[i][k] + l[k][j];
					pre[i][j] = k;
				}
			}

	}
	for(int i=1; i<=n; ++i, cout<<'\n')
		for(int j=1; j<=n; ++j)
			g<<l[i][j]<<' ';
	return 0;
}