Pagini recente » Cod sursa (job #827088) | Cod sursa (job #2086633) | Cod sursa (job #1363139) | Cod sursa (job #2129708) | Cod sursa (job #3245370)
#include <fstream>
#include <vector>
int main()
{
std::ifstream in("royfloyd.in");
int n; in >> n;
std::vector adjancencyMatrix(n, std::vector(n, 0));
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
in >> adjancencyMatrix[i][j];
in.close();
std::vector dist(n, std::vector(n, 0));
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
dist[i][j] = adjancencyMatrix[i][j];
for(int k = 0; k < n; k++)
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(adjancencyMatrix[i][k] != 0 && adjancencyMatrix[k][j] != 0 && (dist[i][j] == 0 || dist[i][j] > dist[i][k] + dist[k][j]) && i != j)
dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);
std::ofstream out("royfloyd.out");
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
out << dist[i][j] << " ";
out << "\n";
}
out.close();
return 0;
}