Cod sursa(job #2202004)

Utilizator larisamLarisa Togoe larisam Data 6 mai 2018 22:10:53
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.77 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

const int kNmax = 101;

class Task {
public:
	void solve() {
		read_input();
		compute();
		print_output();
	}

private:
	int n;
	int d[kNmax][kNmax];
	int a[kNmax][kNmax];

	void read_input() {
		ifstream fin("royfloyd.in");
		fin >> n;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				fin >> a[i][j];
			}
		}
		fin.close();
	}

	void compute() {
		/*
		TODO: Gasiti distantele minime intre oricare doua noduri, folosind RoyFloyd
		pe graful orientat cu n noduri, m arce stocat in matricea ponderilor a
		(declarata mai sus).

		Atentie:
		O muchie (x, y, w) este reprezentata astfel in matricea ponderilor:
			a[x][y] = w;
		Daca nu exista o muchie intre doua noduri x si y, in matricea ponderilor
		se va afla valoarea 0:
			a[x][y] = 0;

		Trebuie sa populati matricea d[][] (declarata mai sus):
			d[x][y] = distanta minima intre nodurile x si y, daca exista drum.
			d[x][y] = 0 daca nu exista drum intre x si y.
			d[x][x] = 0.
		*/

		int i, j, k;
		for (i = 1; i <= n; i++) {
			for (j = 1; j<= n; j++) {
				if (a[i][j] == 0)
					d[i][j] = 1<<29;
				else d[i][j] = a[i][j];
			}
			d[i][i] = 0;
		}

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

		for (i = 1; i <= n; i++){
			for (j = 1; j <= n; j++){
				if (d[i][j] >= 1<<29)
					d[i][j] = 0;
			}
		}


	}


	void print_output() {
		ofstream fout("royfloyd.out");
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				fout << d[i][j] << ' ';
			}
			fout << '\n';
		}
		fout.close();
	}
};

int main() {
	Task *task = new Task();
	task->solve();
	delete task;
	return 0;
}