Cod sursa(job #1458224)

Utilizator glbglGeorgiana bgl glbgl Data 7 iulie 2015 10:10:08
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <stdio.h>
#include <vector>
#include <iostream>
#include <fstream>

#define MAX 100

using namespace std;

ifstream in("royfloyd.in");
ofstream out("royfloyd.out");

int N;
vector <vector<int> > tpl;

void read(){

	in >> N;

	tpl.resize(N+1, vector<int>(N+1));
	

	int x;

	for(int i = 1; i <= N; ++i){
		for(int j = 1; j <= N; ++j){
			in >> x;
			tpl[i][j] = x;
		}
		
	}

}


void RoyFloyd(){

	for(int k = 1; k <= N; ++k){
		for(int i = 1; i <= N; ++i){
			for(int j = 1; j <=N; ++j){
				if(i != j && tpl[i][k] != 0 && tpl[k][j] != 0){
					int dist = tpl[i][k] + tpl[k][j];
					if(tpl[i][j] > dist || tpl[i][j] == 0)
						tpl[i][j] = dist;
				}
			}
		}
	}
}


void write(){

	for(int i = 1; i <= N; ++i){
		for(int j = 1; j <= N; ++j){
			out << tpl[i][j] << " ";
		}
		out << "\n";
	}
}



int main(){

	read();
	RoyFloyd();
	write();
	
}