Pagini recente » Cod sursa (job #1407589) | Rating Ciumbarlescoc Eu (probezprobleme) | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #2636633)
#include <fstream>
#include <vector>
#include <unordered_map>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
void readMatrix(int& n, long long int**& adjMatrix) {
std::ifstream inputStream{ "royfloyd.in" };
inputStream >> n;
adjMatrix = new long long int* [n];
for (int i = 0; i < n; i++)
adjMatrix[i] = new long long int[n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
inputStream >> adjMatrix[i][j];
inputStream.close();
}
void outputMatrix(int n, long long int** matrix) {
std::ofstream outputStream{ "royfloyd.out" };
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
outputStream << matrix[i][j] << " ";
outputStream << "\n";
}
}
void floydWarshall(int n, long long int** adjMatrix) {
for (int k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (adjMatrix[i][j] > adjMatrix[i][k] + adjMatrix[k][j])
adjMatrix[i][j] = adjMatrix[i][k] + adjMatrix[k][j];
}
}
}
}
int main() {
int n;
long long int** adjMatrix = nullptr;
readMatrix(n, adjMatrix);
floydWarshall(n, adjMatrix);
outputMatrix(n, adjMatrix);
}