-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloyd_warshall.cpp
64 lines (56 loc) · 1.41 KB
/
floyd_warshall.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <cmath>
int main(int argc, char* argv[]) {
if (argc < 4) {
std::cerr << "Usage: " << argv[0] << " distances_file distances_export_file path_export_file";
return -1;
}
std::vector<std::vector<double> > dist;
std::ifstream source(argv[1]);
std::string input;
double x;
while (std::getline(source, input)) {
std::stringstream stream(input);
std::vector<double> newl;
while (stream >> x) newl.push_back(x);
dist.push_back(newl);
}
std::vector<std::vector<int> > prev;
for (int i = 0; i < dist.size(); i++) {
prev.push_back(std::vector<int>());
for (int j = 0; j < dist.size(); j++) {
prev[i].push_back(j);
}
}
for (int k = 0; k < dist.size(); k++) {
for (int i = 0; i < dist.size(); i++) {
for (int j = 0; j < dist.size(); j++) {
if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
prev[i][j] = prev[i][k];
}
}
}
std::cout << k << "/" << dist.size() << std::endl;
}
std::ofstream output(argv[2]);
for (int i = 0; i < dist.size(); i++) {
for (int j = 0; j < dist.size(); j++) {
output << dist[i][j] << " ";
}
output << std::endl;
}
output.close();
output.open(argv[3]);
for (int i = 0; i < prev.size(); i++) {
for (int j = 0; j < prev.size(); j++) {
output << prev[i][j] << " ";
}
output << std::endl;
}
output.close();
return 0;
}