Pagini recente » Cod sursa (job #2124613) | Cod sursa (job #1064667) | Cod sursa (job #22488) | Cod sursa (job #275085) | Cod sursa (job #1422692)
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>
const char *in_file_name = "royfloyd.in";
const char *out_file_name = "royfloyd.out";
using namespace std;
ifstream in_file;
ofstream out_file;
int N, **edges;
void die(bool assertion, const char *message)
{
if (assertion) {
fprintf(stderr, "(%s, %d): ",__FILE__, __LINE__);
perror(message);
exit(EXIT_FAILURE);
}
}
void read_file()
{
in_file >> N;
edges = new int*[N];
for (int i = 0; i < N; ++i)
edges[i] = new int[N];
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
in_file >> edges[i][j];
}
void roy_floyd()
{
for (int k = 0; k < N; k++) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (edges[i][j] > edges[i][k] + edges[k][j])
edges[i][j] = edges[i][k] + edges[k][j];
}
}
}
}
void write_file()
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
out_file << edges[i][j] << ' ';
}
out_file << endl;
}
}
int main()
{
in_file.open(in_file_name, ios::in);
die(!in_file, "Error opening file for reading");
out_file.open(out_file_name, ios::out);
die(!out_file, "Error opening file for writing");
read_file();
roy_floyd();
write_file();
in_file.close();
out_file.close();
for (int i = 0; i < N; i++)
delete []edges[i];
delete []edges;
return 0;
}