From 7af7223c94e4a6fc05618a8f368bc262cedcaa91 Mon Sep 17 00:00:00 2001 From: wheson Date: Sat, 31 Mar 2018 17:32:11 +0900 Subject: [PATCH 01/17] =?UTF-8?q?Dijkstra=E3=82=AF=E3=83=A9=E3=82=B9?= =?UTF-8?q?=E3=81=AE=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Graph/dijkstra.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 lib/Graph/dijkstra.cpp diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp new file mode 100644 index 0000000..0bc2702 --- /dev/null +++ b/lib/Graph/dijkstra.cpp @@ -0,0 +1,79 @@ +#include "template.h" + +struct Edge { + long long cost; + int to; + Edge(int t, long long c) { + cost = c; + to = t; + } + bool operator<(const Edge &e) const { return cost < e.cost; } + bool operator>(const Edge &e) const { return cost > e.cost; } +}; + +class Dijkstra { + private: + bool is_dir = false; // 無向グラフ: false, 有向グラフ: true + long long INFl = (long long)1e15; + int array_size_of_cost; + vector> adj; // adj[始点][動的配列で始点から伸びる枝] + + public: + Dijkstra(int n, bool dir); + vector cost; + vector prever; + void add_edge(int f, int t, long long c); + bool has_path(int t); // tに至るパスはあるか + vector get_shortest_path(int t); + void run(int f); +}; + +Dijkstra::Dijkstra(int n, bool dir) + : is_dir(dir), + array_size_of_cost(n + 1), + adj(vector>(array_size_of_cost)), + cost(vector(array_size_of_cost)), + prever(vector(array_size_of_cost, -1)) { + fill(cost.begin(), cost.end(), INFl); +} + +void Dijkstra::add_edge(int f, int t, long long c) { + adj[f].push_back(Edge(t, c)); + if (!is_dir) adj[t].push_back(Edge(f, c)); +} + +bool Dijkstra::has_path(int t) { return cost[t] != INFl; } + +vector Dijkstra::get_shortest_path(int t) { + vector path; + for (; t != -1; t = prever[t]) path.push_back(t); + + reverse(path.begin(), path.end()); + return path; +} + +void Dijkstra::run(int first_state) { + using State = Edge; + priority_queue, greater> pq; + + cost[first_state] = 0; + pq.push(Edge(first_state, 0LL)); + + while (!pq.empty()) { + State current_state = pq.top(); + pq.pop(); + + if (cost[current_state.to] < current_state.cost) continue; + + for (int i = 0; i < adj[current_state.to].size(); i++) { + State tmp = adj[current_state.to][i]; + + long long sum_cost = current_state.cost + tmp.cost; + if (cost[tmp.to] > sum_cost) { + cost[tmp.to] = sum_cost; + prever[tmp.to] = current_state.to; + pq.push(Edge(tmp.to, cost[tmp.to])); + } + } + } +} From 8346a4fbbb566a8990b7eab8808d7d95fbfb0a9b Mon Sep 17 00:00:00 2001 From: wheson Date: Sat, 31 Mar 2018 18:59:29 +0900 Subject: [PATCH 02/17] =?UTF-8?q?=E5=91=BD=E5=90=8D=E8=A6=8F=E5=89=87?= =?UTF-8?q?=E9=80=9A=E3=82=8A=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Graph/dijkstra.cpp | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp index 0bc2702..1dcbed0 100644 --- a/lib/Graph/dijkstra.cpp +++ b/lib/Graph/dijkstra.cpp @@ -1,9 +1,9 @@ #include "template.h" struct Edge { - long long cost; + Int cost; int to; - Edge(int t, long long c) { + Edge(int t, Int c) { cost = c; to = t; } @@ -13,38 +13,38 @@ struct Edge { class Dijkstra { private: - bool is_dir = false; // 無向グラフ: false, 有向グラフ: true - long long INFl = (long long)1e15; - int array_size_of_cost; + bool isDir = false; // 無向グラフ: false, 有向グラフ: true + Int INFl = (Int)1e15; + int arraySizeOfCost; vector> adj; // adj[始点][動的配列で始点から伸びる枝] public: Dijkstra(int n, bool dir); - vector cost; + vector cost; vector prever; - void add_edge(int f, int t, long long c); - bool has_path(int t); // tに至るパスはあるか - vector get_shortest_path(int t); - void run(int f); + void AddEdge(int f, int t, Int c); + bool HasPath(int t); // tに至るパスはあるか + vector getShortestPath(int t); + void Run(int f); }; Dijkstra::Dijkstra(int n, bool dir) - : is_dir(dir), - array_size_of_cost(n + 1), - adj(vector>(array_size_of_cost)), - cost(vector(array_size_of_cost)), - prever(vector(array_size_of_cost, -1)) { + : isDir(dir), + arraySizeOfCost(n + 1), + adj(vector>(arraySizeOfCost)), + cost(vector(arraySizeOfCost)), + prever(vector(arraySizeOfCost, -1)) { fill(cost.begin(), cost.end(), INFl); } -void Dijkstra::add_edge(int f, int t, long long c) { +void Dijkstra::AddEdge(int f, int t, Int c) { adj[f].push_back(Edge(t, c)); - if (!is_dir) adj[t].push_back(Edge(f, c)); + if (!isDir) adj[t].push_back(Edge(f, c)); } -bool Dijkstra::has_path(int t) { return cost[t] != INFl; } +bool Dijkstra::HasPath(int t) { return cost[t] != INFl; } -vector Dijkstra::get_shortest_path(int t) { +vector Dijkstra::getShortestPath(int t) { vector path; for (; t != -1; t = prever[t]) path.push_back(t); @@ -52,7 +52,7 @@ vector Dijkstra::get_shortest_path(int t) { return path; } -void Dijkstra::run(int first_state) { +void Dijkstra::Run(int first_state) { using State = Edge; priority_queue, greater> pq; @@ -68,7 +68,7 @@ void Dijkstra::run(int first_state) { for (int i = 0; i < adj[current_state.to].size(); i++) { State tmp = adj[current_state.to][i]; - long long sum_cost = current_state.cost + tmp.cost; + Int sum_cost = current_state.cost + tmp.cost; if (cost[tmp.to] > sum_cost) { cost[tmp.to] = sum_cost; prever[tmp.to] = current_state.to; From 1549b3f7fc8b03d7167413531feb3c218b47cc1e Mon Sep 17 00:00:00 2001 From: wheson Date: Sat, 31 Mar 2018 20:42:42 +0900 Subject: [PATCH 03/17] =?UTF-8?q?=E7=B6=BA=E9=BA=97=E3=81=AB=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Graph/dijkstra.cpp | 54 +++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp index 1dcbed0..0610acc 100644 --- a/lib/Graph/dijkstra.cpp +++ b/lib/Graph/dijkstra.cpp @@ -1,9 +1,8 @@ #include "template.h" struct Edge { - Int cost; - int to; - Edge(int t, Int c) { + int cost, to; + Edge(int t, int c) { cost = c; to = t; } @@ -14,30 +13,30 @@ struct Edge { class Dijkstra { private: bool isDir = false; // 無向グラフ: false, 有向グラフ: true - Int INFl = (Int)1e15; - int arraySizeOfCost; + long long INFl = (long long)1e15; + int V; // 頂点数 vector> adj; // adj[始点][動的配列で始点から伸びる枝] + vector prever; public: Dijkstra(int n, bool dir); - vector cost; - vector prever; - void AddEdge(int f, int t, Int c); - bool HasPath(int t); // tに至るパスはあるか - vector getShortestPath(int t); + vector cost; + void AddEdge(int f, int t, int c); + bool HasPath(int t); // tに至るパスはあるか + vector getShortestPath(int t); // tへの最短路 void Run(int f); }; Dijkstra::Dijkstra(int n, bool dir) : isDir(dir), - arraySizeOfCost(n + 1), - adj(vector>(arraySizeOfCost)), - cost(vector(arraySizeOfCost)), - prever(vector(arraySizeOfCost, -1)) { + V(n + 1), + adj(vector>(V)), + prever(vector(V, -1)), + cost(vector(V)) { fill(cost.begin(), cost.end(), INFl); } -void Dijkstra::AddEdge(int f, int t, Int c) { +void Dijkstra::AddEdge(int f, int t, int c) { adj[f].push_back(Edge(t, c)); if (!isDir) adj[t].push_back(Edge(f, c)); } @@ -52,26 +51,23 @@ vector Dijkstra::getShortestPath(int t) { return path; } -void Dijkstra::Run(int first_state) { - using State = Edge; - priority_queue, greater> pq; +void Dijkstra::Run(int firstNode) { + priority_queue, greater> pq; - cost[first_state] = 0; - pq.push(Edge(first_state, 0LL)); + cost[firstNode] = 0; + pq.push(Edge(firstNode, 0LL)); while (!pq.empty()) { - State current_state = pq.top(); + Edge currentEdge = pq.top(); pq.pop(); - if (cost[current_state.to] < current_state.cost) continue; - - for (int i = 0; i < adj[current_state.to].size(); i++) { - State tmp = adj[current_state.to][i]; + if (cost[currentEdge.to] < currentEdge.cost) continue; - Int sum_cost = current_state.cost + tmp.cost; - if (cost[tmp.to] > sum_cost) { - cost[tmp.to] = sum_cost; - prever[tmp.to] = current_state.to; + for (Edge tmp : adj[currentEdge.to]) { + long long sumCost = currentEdge.cost + tmp.cost; + if (cost[tmp.to] > sumCost) { + cost[tmp.to] = sumCost; + prever[tmp.to] = currentEdge.to; pq.push(Edge(tmp.to, cost[tmp.to])); } } From b451b8122b292f75decccd0ea4729f94263d8637 Mon Sep 17 00:00:00 2001 From: wheson Date: Sat, 31 Mar 2018 20:46:13 +0900 Subject: [PATCH 04/17] long long -> Int --- lib/Graph/dijkstra.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp index 0610acc..91a0dc6 100644 --- a/lib/Graph/dijkstra.cpp +++ b/lib/Graph/dijkstra.cpp @@ -13,14 +13,14 @@ struct Edge { class Dijkstra { private: bool isDir = false; // 無向グラフ: false, 有向グラフ: true - long long INFl = (long long)1e15; + Int INFl = (Int)1e15; int V; // 頂点数 vector> adj; // adj[始点][動的配列で始点から伸びる枝] vector prever; public: Dijkstra(int n, bool dir); - vector cost; + vector cost; void AddEdge(int f, int t, int c); bool HasPath(int t); // tに至るパスはあるか vector getShortestPath(int t); // tへの最短路 @@ -32,7 +32,7 @@ Dijkstra::Dijkstra(int n, bool dir) V(n + 1), adj(vector>(V)), prever(vector(V, -1)), - cost(vector(V)) { + cost(vector(V)) { fill(cost.begin(), cost.end(), INFl); } @@ -64,7 +64,7 @@ void Dijkstra::Run(int firstNode) { if (cost[currentEdge.to] < currentEdge.cost) continue; for (Edge tmp : adj[currentEdge.to]) { - long long sumCost = currentEdge.cost + tmp.cost; + Int sumCost = currentEdge.cost + tmp.cost; if (cost[tmp.to] > sumCost) { cost[tmp.to] = sumCost; prever[tmp.to] = currentEdge.to; From a9a4ea09879faecad01d6cbee3c4ce2655650b19 Mon Sep 17 00:00:00 2001 From: wheson Date: Sun, 1 Apr 2018 17:04:12 +0900 Subject: [PATCH 05/17] =?UTF-8?q?template=E3=82=92=E4=BD=BF=E3=81=A3?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Graph/dijkstra.cpp | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp index 91a0dc6..9ed8f5f 100644 --- a/lib/Graph/dijkstra.cpp +++ b/lib/Graph/dijkstra.cpp @@ -1,49 +1,46 @@ #include "template.h" -struct Edge { - int cost, to; - Edge(int t, int c) { - cost = c; - to = t; - } - bool operator<(const Edge &e) const { return cost < e.cost; } - bool operator>(const Edge &e) const { return cost > e.cost; } -}; - +template class Dijkstra { private: bool isDir = false; // 無向グラフ: false, 有向グラフ: true - Int INFl = (Int)1e15; + T INF = numeric_limits::max(); int V; // 頂点数 vector> adj; // adj[始点][動的配列で始点から伸びる枝] vector prever; public: Dijkstra(int n, bool dir); - vector cost; + vector cost; void AddEdge(int f, int t, int c); bool HasPath(int t); // tに至るパスはあるか vector getShortestPath(int t); // tへの最短路 void Run(int f); }; -Dijkstra::Dijkstra(int n, bool dir) +template +Dijkstra::Dijkstra(int n, bool dir) : isDir(dir), V(n + 1), adj(vector>(V)), prever(vector(V, -1)), - cost(vector(V)) { - fill(cost.begin(), cost.end(), INFl); + cost(vector(V)) { + fill(cost.begin(), cost.end(), INF); } -void Dijkstra::AddEdge(int f, int t, int c) { +template +void Dijkstra::AddEdge(int f, int t, int c) { adj[f].push_back(Edge(t, c)); if (!isDir) adj[t].push_back(Edge(f, c)); } -bool Dijkstra::HasPath(int t) { return cost[t] != INFl; } +template +bool Dijkstra::HasPath(int t) { + return cost[t] != INF; +} -vector Dijkstra::getShortestPath(int t) { +template +vector Dijkstra::getShortestPath(int t) { vector path; for (; t != -1; t = prever[t]) path.push_back(t); @@ -51,7 +48,8 @@ vector Dijkstra::getShortestPath(int t) { return path; } -void Dijkstra::Run(int firstNode) { +template +void Dijkstra::Run(int firstNode) { priority_queue, greater> pq; cost[firstNode] = 0; @@ -64,7 +62,7 @@ void Dijkstra::Run(int firstNode) { if (cost[currentEdge.to] < currentEdge.cost) continue; for (Edge tmp : adj[currentEdge.to]) { - Int sumCost = currentEdge.cost + tmp.cost; + T sumCost = currentEdge.cost + tmp.cost; if (cost[tmp.to] > sumCost) { cost[tmp.to] = sumCost; prever[tmp.to] = currentEdge.to; From 4f0b789c589076fe4532a363d0f49a0413a3279f Mon Sep 17 00:00:00 2001 From: wheson Date: Sun, 1 Apr 2018 17:05:53 +0900 Subject: [PATCH 06/17] =?UTF-8?q?Edge=E5=BF=98=E3=82=8C=E3=81=A6=E3=82=8B?= =?UTF-8?q?=E3=81=98=E3=82=83=E3=82=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Graph/dijkstra.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp index 9ed8f5f..072ed4d 100644 --- a/lib/Graph/dijkstra.cpp +++ b/lib/Graph/dijkstra.cpp @@ -1,5 +1,15 @@ #include "template.h" +struct Edge { + int cost, to; + Edge(int t, int c) { + cost = c; + to = t; + } + bool operator<(const Edge &e) const { return cost < e.cost; } + bool operator>(const Edge &e) const { return cost > e.cost; } +}; + template class Dijkstra { private: From c918ee01d0f332d44459e6d0eb0b26a915e2553e Mon Sep 17 00:00:00 2001 From: wheson Date: Sun, 1 Apr 2018 18:51:18 +0900 Subject: [PATCH 07/17] =?UTF-8?q?Edge=E3=81=AE=E3=82=B3=E3=83=B3=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=83=A9=E3=82=AF=E3=82=BF=E3=82=92=E3=82=A4=E3=82=B1?= =?UTF-8?q?=E3=82=A4=E3=82=B1=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Graph/dijkstra.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp index 072ed4d..34b65fb 100644 --- a/lib/Graph/dijkstra.cpp +++ b/lib/Graph/dijkstra.cpp @@ -1,11 +1,9 @@ #include "template.h" struct Edge { - int cost, to; - Edge(int t, int c) { - cost = c; - to = t; - } + int to, cost; + Edge(int to, int cost) : to(to), cost(cost) {} + bool operator<(const Edge &e) const { return cost < e.cost; } bool operator>(const Edge &e) const { return cost > e.cost; } }; From 6614d5d0d38f7656415a7d00d766465bc34e6b9c Mon Sep 17 00:00:00 2001 From: wheson Date: Sat, 28 Apr 2018 12:45:50 +0900 Subject: [PATCH 08/17] =?UTF-8?q?graph.h=E3=82=92=E5=88=A9=E7=94=A8?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Graph/dijkstra.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp index 34b65fb..330a42b 100644 --- a/lib/Graph/dijkstra.cpp +++ b/lib/Graph/dijkstra.cpp @@ -1,12 +1,6 @@ #include "template.h" +#include "graph.h" -struct Edge { - int to, cost; - Edge(int to, int cost) : to(to), cost(cost) {} - - bool operator<(const Edge &e) const { return cost < e.cost; } - bool operator>(const Edge &e) const { return cost > e.cost; } -}; template class Dijkstra { From 4bd8c1355bbe35dabeaf06e08deb2850cd4aad4d Mon Sep 17 00:00:00 2001 From: wheson Date: Sat, 28 Apr 2018 12:47:05 +0900 Subject: [PATCH 09/17] =?UTF-8?q?graph.h=E3=82=92=E5=88=A9=E7=94=A8?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=81=AB=E8=BE=BA=E3=82=8A=E5=9E=8B=E3=81=AE?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Graph/dijkstra.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp index 330a42b..787840a 100644 --- a/lib/Graph/dijkstra.cpp +++ b/lib/Graph/dijkstra.cpp @@ -8,7 +8,7 @@ class Dijkstra { bool isDir = false; // 無向グラフ: false, 有向グラフ: true T INF = numeric_limits::max(); int V; // 頂点数 - vector> adj; // adj[始点][動的配列で始点から伸びる枝] + AdjList adj; // adj[始点][動的配列で始点から伸びる枝] vector prever; public: @@ -24,16 +24,16 @@ template Dijkstra::Dijkstra(int n, bool dir) : isDir(dir), V(n + 1), - adj(vector>(V)), + adj(V), prever(vector(V, -1)), - cost(vector(V)) { + cost(V) { fill(cost.begin(), cost.end(), INF); } template void Dijkstra::AddEdge(int f, int t, int c) { - adj[f].push_back(Edge(t, c)); - if (!isDir) adj[t].push_back(Edge(f, c)); + adj[f].push_back(Edge(t, c)); + if (!isDir) adj[t].push_back(Edge(f, c)); } template From 65555e7187a6a043eb6ad2973d93ae71812efc88 Mon Sep 17 00:00:00 2001 From: wheson Date: Sat, 28 Apr 2018 12:48:39 +0900 Subject: [PATCH 10/17] =?UTF-8?q?priority=5Fqueue=E3=81=ABEdge=E3=81=A7?= =?UTF-8?q?=E3=81=AF=E3=81=AA=E3=81=8FPi=E3=82=92=E5=85=A5=E3=82=8C?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E4=BF=AE=E6=AD=A3(graph.h=E3=81=A7?= =?UTF-8?q?=E3=81=AF=E6=AF=94=E8=BC=83=E6=BC=94=E7=AE=97=E5=AD=90=E3=81=A7?= =?UTF-8?q?=E3=81=AE=E5=87=A6=E7=90=86=E3=82=92=E8=A8=98=E8=BF=B0=E3=81=97?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=AA=E3=81=84=E3=81=9F=E3=82=81)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Graph/dijkstra.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp index 787840a..5d3887a 100644 --- a/lib/Graph/dijkstra.cpp +++ b/lib/Graph/dijkstra.cpp @@ -52,24 +52,25 @@ vector Dijkstra::getShortestPath(int t) { template void Dijkstra::Run(int firstNode) { - priority_queue, greater> pq; + using Pi = pair; + priority_queue, greater> pq; cost[firstNode] = 0; - pq.push(Edge(firstNode, 0LL)); + pq.push(Pi(cost[firstNode], firstNode)); while (!pq.empty()) { - Edge currentEdge = pq.top(); + Pi currentEdge = pq.top(); pq.pop(); + if (cost[currentEdge.second] < currentEdge.first) continue; - if (cost[currentEdge.to] < currentEdge.cost) continue; - - for (Edge tmp : adj[currentEdge.to]) { - T sumCost = currentEdge.cost + tmp.cost; + for (Edge tmp : adj[currentEdge.second]) { + T sumCost = currentEdge.first + tmp.cost; if (cost[tmp.to] > sumCost) { cost[tmp.to] = sumCost; - prever[tmp.to] = currentEdge.to; - pq.push(Edge(tmp.to, cost[tmp.to])); + prever[tmp.to] = currentEdge.second; + pq.push(Pi(cost[tmp.to], tmp.to)); } } } } + From fa51b05f43a79311857c7740527f1a133f490e55 Mon Sep 17 00:00:00 2001 From: wheson Date: Sat, 28 Apr 2018 12:50:23 +0900 Subject: [PATCH 11/17] make format --- lib/Graph/dijkstra.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp index 5d3887a..e4152d5 100644 --- a/lib/Graph/dijkstra.cpp +++ b/lib/Graph/dijkstra.cpp @@ -1,13 +1,12 @@ -#include "template.h" #include "graph.h" - +#include "template.h" template class Dijkstra { private: bool isDir = false; // 無向グラフ: false, 有向グラフ: true T INF = numeric_limits::max(); - int V; // 頂点数 + int V; // 頂点数 AdjList adj; // adj[始点][動的配列で始点から伸びる枝] vector prever; @@ -22,11 +21,7 @@ class Dijkstra { template Dijkstra::Dijkstra(int n, bool dir) - : isDir(dir), - V(n + 1), - adj(V), - prever(vector(V, -1)), - cost(V) { + : isDir(dir), V(n + 1), adj(V), prever(vector(V, -1)), cost(V) { fill(cost.begin(), cost.end(), INF); } @@ -73,4 +68,3 @@ void Dijkstra::Run(int firstNode) { } } } - From 9ce4e9923bc850fe037fc2a8d26b5d061e890160 Mon Sep 17 00:00:00 2001 From: wheson Date: Sat, 28 Apr 2018 12:52:50 +0900 Subject: [PATCH 12/17] =?UTF-8?q?max=E3=81=8B=E3=82=8910=E3=82=92=E5=89=B2?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Graph/dijkstra.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp index e4152d5..b149b29 100644 --- a/lib/Graph/dijkstra.cpp +++ b/lib/Graph/dijkstra.cpp @@ -5,7 +5,7 @@ template class Dijkstra { private: bool isDir = false; // 無向グラフ: false, 有向グラフ: true - T INF = numeric_limits::max(); + T INF = numeric_limits::max() / 10; int V; // 頂点数 AdjList adj; // adj[始点][動的配列で始点から伸びる枝] vector prever; From a9864d7d5183bf2f7c6bb9bbdbdd86b42f9a6f03 Mon Sep 17 00:00:00 2001 From: wheson Date: Sat, 28 Apr 2018 12:55:13 +0900 Subject: [PATCH 13/17] =?UTF-8?q?=E5=91=BD=E5=90=8D=E8=A6=8F=E5=89=87?= =?UTF-8?q?=E3=81=AB=E5=BE=93=E3=81=A3=E3=81=A6=E3=81=84=E3=81=AA=E3=81=8B?= =?UTF-8?q?=E3=81=A3=E3=81=9F=E3=81=AE=E3=81=A7=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Graph/dijkstra.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.cpp index b149b29..12b084e 100644 --- a/lib/Graph/dijkstra.cpp +++ b/lib/Graph/dijkstra.cpp @@ -15,7 +15,7 @@ class Dijkstra { vector cost; void AddEdge(int f, int t, int c); bool HasPath(int t); // tに至るパスはあるか - vector getShortestPath(int t); // tへの最短路 + vector GetShortestPath(int t); // tへの最短路 void Run(int f); }; @@ -37,7 +37,7 @@ bool Dijkstra::HasPath(int t) { } template -vector Dijkstra::getShortestPath(int t) { +vector Dijkstra::GetShortestPath(int t) { vector path; for (; t != -1; t = prever[t]) path.push_back(t); From 158c542af56c64829468649a71871c0fda9583c7 Mon Sep 17 00:00:00 2001 From: wheson Date: Sat, 28 Apr 2018 19:11:44 +0900 Subject: [PATCH 14/17] .cpp -> .h --- lib/Graph/{dijkstra.cpp => dijkstra.h} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lib/Graph/{dijkstra.cpp => dijkstra.h} (100%) diff --git a/lib/Graph/dijkstra.cpp b/lib/Graph/dijkstra.h similarity index 100% rename from lib/Graph/dijkstra.cpp rename to lib/Graph/dijkstra.h From 5b2e627c90b3def9cb04eb75790d5342ad8e2044 Mon Sep 17 00:00:00 2001 From: wheson Date: Sat, 28 Apr 2018 19:12:10 +0900 Subject: [PATCH 15/17] #pragma once --- lib/Graph/dijkstra.h | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Graph/dijkstra.h b/lib/Graph/dijkstra.h index 12b084e..94e9bb4 100644 --- a/lib/Graph/dijkstra.h +++ b/lib/Graph/dijkstra.h @@ -1,3 +1,4 @@ +#pragma once #include "graph.h" #include "template.h" From d18f380f870407421eb2eb592ce6159d20012cd3 Mon Sep 17 00:00:00 2001 From: wheson Date: Mon, 30 Apr 2018 23:05:51 +0900 Subject: [PATCH 16/17] =?UTF-8?q?=E6=9C=89=E5=90=91=E3=81=8B=E7=84=A1?= =?UTF-8?q?=E5=90=91=E3=81=8B=E3=81=AE=E6=8C=87=E5=AE=9A=E3=82=92=E6=B6=88?= =?UTF-8?q?=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Graph/dijkstra.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/Graph/dijkstra.h b/lib/Graph/dijkstra.h index 94e9bb4..37cca2d 100644 --- a/lib/Graph/dijkstra.h +++ b/lib/Graph/dijkstra.h @@ -5,14 +5,13 @@ template class Dijkstra { private: - bool isDir = false; // 無向グラフ: false, 有向グラフ: true T INF = numeric_limits::max() / 10; int V; // 頂点数 AdjList adj; // adj[始点][動的配列で始点から伸びる枝] vector prever; public: - Dijkstra(int n, bool dir); + Dijkstra(int n); vector cost; void AddEdge(int f, int t, int c); bool HasPath(int t); // tに至るパスはあるか @@ -21,15 +20,14 @@ class Dijkstra { }; template -Dijkstra::Dijkstra(int n, bool dir) - : isDir(dir), V(n + 1), adj(V), prever(vector(V, -1)), cost(V) { +Dijkstra::Dijkstra(int n) + : V(n + 1), adj(V), prever(vector(V, -1)), cost(V) { fill(cost.begin(), cost.end(), INF); } template void Dijkstra::AddEdge(int f, int t, int c) { adj[f].push_back(Edge(t, c)); - if (!isDir) adj[t].push_back(Edge(f, c)); } template From 1b5fe2d759bacb0bc31085c2a99104531aa72300 Mon Sep 17 00:00:00 2001 From: wheson Date: Mon, 30 Apr 2018 23:13:31 +0900 Subject: [PATCH 17/17] explicit --- lib/Graph/dijkstra.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Graph/dijkstra.h b/lib/Graph/dijkstra.h index 37cca2d..8da9536 100644 --- a/lib/Graph/dijkstra.h +++ b/lib/Graph/dijkstra.h @@ -11,7 +11,7 @@ class Dijkstra { vector prever; public: - Dijkstra(int n); + explicit Dijkstra(int n); vector cost; void AddEdge(int f, int t, int c); bool HasPath(int t); // tに至るパスはあるか