Pagini recente » Istoria paginii runda/ag/clasament | Cod sursa (job #2139541) | Cod sursa (job #478212) | Cod sursa (job #2754892) | Cod sursa (job #2422468)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
void getAllShortestPaths(int** mat, int size);
int main() {
int** dist;
int nrOfVertices;
fin >> nrOfVertices;
dist = (int**)malloc(sizeof(int*) * nrOfVertices);
for (int i = 0; i < nrOfVertices; ++i) {
dist[i] = (int*)malloc(sizeof(int) * nrOfVertices);
for (int j = 0; j < nrOfVertices; ++j) {
fin >> dist[i][j];
}
}
getAllShortestPaths(dist, nrOfVertices);
for (int i = 0; i < nrOfVertices; ++i) {
for (int j = 0; j < nrOfVertices; ++j) {
fout << dist[i][j] << ' ';
}
fout << '\n';
free(dist[i]);
}
free(dist);
fin.close();
fout.close();
return 0;
}
void getAllShortestPaths(int** mat, int size) {
for (int k = 0; k < size; ++k) {
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
if (mat[i][j] > mat[i][k] + mat[k][j]) {
mat[i][j] = mat[i][k] + mat[k][j];
}
}
}
}
}