Pagini recente » Cod sursa (job #562807) | Cod sursa (job #1513110) | Cod sursa (job #2120048) | Cod sursa (job #3222421) | Cod sursa (job #2432666)
#include <fstream>
#include <vector>
#include <string>
using namespace std;
string const inFile = "royfloyd.in";
string const outFile = "royfloyd.out";
ifstream Read(inFile);
ofstream Write(outFile);
int main() {
unsigned n;
vector<vector<int>> dist;
unsigned i, j, k;
Read >> n;
dist.resize(n, vector<int>(n));
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j) {
Read >> dist[i][j];
}
for (k = 0; k < n; ++k)
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j) {
if (i != j)
if (dist[i][k] != 0)
if (dist[k][j] != 0) {
if (dist[i][j] == 0) {
dist[i][j] = dist[i][k] + dist[k][j];
}
else if (dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
Write << dist[i][j] << ' ';
}
Write << '\n';
}
return 0;
}