Cod sursa(job #1799171)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 5 noiembrie 2016 21:10:07
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

const int INF = 0x3f3f3f3f;

void royFloyd(vector<vector<int>>& cost) {
	const int n = int(cost.size());
	for (int k = 0; k < n; ++k) {
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
			}
		}
	}
}

int main() {
	ifstream in("royfloyd.in");
	ofstream out("royfloyd.out");
	int n;
	in >> n;
	auto cost = vector<vector<int>>(n, vector<int>(n, INF));
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			in >> cost[i][j];
			if (cost[i][j] == 0) {
				cost[i][j] = INF;
			}
		}
	}
	royFloyd(cost);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			out << (cost[i][j] == INF ? 0 : cost[i][j]) << (j == n - 1 ? " " : "\n");
		}
	}
	in.close();
	out.close();
	return 0;
}