-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cpp
More file actions
134 lines (99 loc) · 3.03 KB
/
Copy pathMain.cpp
File metadata and controls
134 lines (99 loc) · 3.03 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <algorithm>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <vector>
#include <rapidjson/document.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
#include "Graph.hpp"
#include "Dijkstra.hpp"
namespace Json {
static const char *g_graph_file = "graph.json";
static const char *g_journeys_file = "journeys.json";
typedef std::list<std::pair<int, int>> PairList;
typedef std::list<std::list<Node*>> PathList;
std::vector<char> read_file(const std::string &filename) {
std::ifstream file(filename);
if (!file.is_open()) {
throw std::runtime_error("Failed to open file!");
}
file.seekg(0, std::ios::end);
auto len = static_cast<size_t>(file.tellg());
file.seekg(0, std::ios::beg);
std::vector<char> buffer(len + 1, '\0');
file.read(buffer.data(), len);
if (!file) {
throw std::runtime_error("Failed to read the file!");
}
return buffer;
}
PairList read_journeys(const std::string &filename) {
auto buffer = read_file(filename);
rapidjson::Document doc;
doc.Parse(buffer.data());
PairList pairs;
for (rapidjson::SizeType i = 0; i < doc.Size(); ++i) {
pairs.push_back(std::make_pair<int, int>(doc[i]["from"].GetInt(), doc[i]["to"].GetInt()));
}
return pairs;
}
void write_journeys(const PairList &pairs, const PathList &paths) {
rapidjson::Document doc;
doc.SetArray();
auto &alloc = doc.GetAllocator();
auto path = paths.begin();
for (auto &pair : pairs) {
rapidjson::Value o(rapidjson::kObjectType);
o.AddMember("from", rapidjson::Value().SetInt(pair.first), alloc);
o.AddMember("to", rapidjson::Value().SetInt(pair.second), alloc);
rapidjson::Value arr(rapidjson::kArrayType);
for (Node *n : *path) {
arr.PushBack(rapidjson::Value().SetInt(n->id), alloc);
}
o.AddMember("route", arr, alloc);
doc.PushBack(o, alloc);
++path;
}
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
doc.Accept(writer);
std::cout << buffer.GetString() << std::endl;
}
Graph build_graph(const std::string &filename) {
auto buffer = read_file(filename);
rapidjson::Document doc;
doc.Parse(buffer.data());
Graph graph;
for (rapidjson::SizeType i = 0; i < doc.Size(); ++i) {
Node *from_node = graph.get_or_add(doc[i]["from"].GetInt());
Node *to_node = graph.get_or_add(doc[i]["to"].GetInt());
if (from_node == to_node) {
continue;
}
from_node->edges.push_back(Edge{ to_node, doc[i]["weight"].GetInt() });
}
return graph;
}
}
void solve() {
std::ios_base::sync_with_stdio(false);
Graph graph = Json::build_graph(Json::g_graph_file);
Dijkstra dijkstra(graph);
Json::PairList journeys = Json::read_journeys(Json::g_journeys_file);
Json::PathList routes;
for (auto &p : journeys) {
auto path = dijkstra.get_shortest_path(p.first, p.second);
routes.push_back(path);
}
Json::write_journeys(journeys, routes);
}
int main() {
try {
solve();
}
catch (std::exception &e) {
std::cerr << "Failed: " << e.what() << std::endl;
}
//std::cin.get();
}