Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++版のリファクタリング/高速化 #4

Merged
merged 19 commits into from
Jun 22, 2020
Prev Previous commit
Next Next commit
add const
wx257osn2 committed Jun 21, 2020
commit 0340050c555598e371cb81dbf4b6af33755676ef
30 changes: 15 additions & 15 deletions cpp/src/main.cpp
Original file line number Diff line number Diff line change
@@ -32,8 +32,8 @@ NodeIndex get_idx(NodeId id) {
}

void add_edge(NodeId start, NodeId end, Distance distance) {
NodeIndex s = get_idx(start);
NodeIndex e = get_idx(end);
const NodeIndex s = get_idx(start);
const NodeIndex e = get_idx(end);
g.edge[s].push_back({e, distance});
}

@@ -75,7 +75,7 @@ void load() {
float d = 0;
for (int idx=0, pos=0, prev_pos=0; pos <= line.length(); pos++) {
if (line[pos] == ',' || pos == line.length()) {
auto field = line.substr(prev_pos, pos-prev_pos);
const auto field = line.substr(prev_pos, pos-prev_pos);
switch (idx) {
case 2: s = stoi(field); break;
case 3: e = stoi(field); break;
@@ -95,10 +95,10 @@ void load() {
using Visit = std::pair<Distance, NodeIndex>;

std::pair<Distance, std::vector<NodeId>> dijkstra(NodeId start, NodeId end) {
NodeIndex s = get_idx(start);
NodeIndex e = get_idx(end);
const NodeIndex s = get_idx(start);
const NodeIndex e = get_idx(end);

int size = g.idx;
const int size = g.idx;
std::vector<Distance> d(size);
std::vector<NodeIndex> prev(size);

@@ -107,15 +107,15 @@ std::pair<Distance, std::vector<NodeId>> dijkstra(NodeId start, NodeId end) {

int visited = 0;
while (!queue.empty()) {
auto a = queue.top();
const auto a = queue.top();
queue.pop();
Distance distance = a.first;
NodeIndex here = a.second;
const Distance distance = a.first;
const NodeIndex here = a.second;
if (is_debug) std::cout << "visiting: " << here << " distance: " << distance << std::endl;
visited++;
for (Edge e : g.edge[here]) {
NodeIndex to = e.first;
Distance w = distance + e.second;
for (const Edge& e : g.edge[here]) {
const NodeIndex to = e.first;
const Distance w = distance + e.second;
if (d[to] == 0 || w < d[to]) {
prev[to] = here;
d[to] = w;
@@ -143,21 +143,21 @@ int main(int argc, char **argv) {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);

int count = atoi(argv[1]);
const int count = atoi(argv[1]);
is_debug = argc > 2 && std::string(argv[2]) == "debug";

load();
std::cerr << "loaded nodes: " << g.idx << std::endl;

std::pair<Distance, std::vector<NodeId>> result;
for (int i=0; i<count; i++) {
NodeId s = g.idx2id[(i+1) * 1000];
const NodeId s = g.idx2id[(i+1) * 1000];
result = dijkstra(s, g.idx2id[1]);
std::cout << "distance: " << result.first << std::endl;
}

std::cout << "route: ";
for (NodeId id: result.second) {
for (const NodeId id: result.second) {
std::cout << id << " ";
}
std::cout << std::endl;