Pagini recente » Cod sursa (job #212270) | Cod sursa (job #870608) | Cod sursa (job #2041992) | Cod sursa (job #2970121) | Cod sursa (job #1200886)
#include <fstream>
const int INFINITY = 1001;
using namespace std;
void floyd_warshall() {
}
int main(void) {
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
int t;
in >> t;
int w[100][100];
for (int i = 0; i < t; i++) {
for (int j = 0; j < t; j++) {
in >> w[i][j];
if (w[i][j] == 0 && i != j) {
w[i][j] = INFINITY;
}
}
}
//run floyd warshall:
// for any 2 nodes i and j, if the distance going from i to j through
// another node k is smaller than dist(i,j), update dist[i,j]
for (int i = 0; i < t; i++) {
for (int j = 0; j < t; j++) {
for (int k = 0; k < t; k++) {
if (w[i][j] > w[i][k] + w[k][j]) {
w[i][j] = w[i][k] + w[k][j];
}
}
}
}
//output distance matrix
for (int i = 0; i < t; i++) {
for (int j = 0; j < t; j++) {
if (w[i][j] == INFINITY) {
w[i][j] = 0;
}
out << w[i][j] << " ";
}
out << "\n";
}
return 0;
}