-
Notifications
You must be signed in to change notification settings - Fork 0
Dijkstraクラスの作成 #9
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
Open
wheson
wants to merge
17
commits into
master
Choose a base branch
from
add-dijkstra
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7af7223
Dijkstraクラスの作成
wheson 8346a4f
命名規則通りに修正
wheson 1549b3f
綺麗にした
wheson b451b81
long long -> Int
wheson a9a4ea0
templateを使った
wheson 4f0b789
Edge忘れてるじゃん
wheson c918ee0
Edgeのコンストラクタをイケイケに
wheson 6614d5d
graph.hを利用するよう修正
wheson 4bd8c13
graph.hを利用するに辺り型の指定を追加
wheson 65555e7
priority_queueにEdgeではなくPiを入れるよう修正(graph.hでは比較演算子での処理を記述していないため)
wheson fa51b05
make format
wheson 9ce4e99
maxから10を割る
wheson a9864d7
命名規則に従っていなかったので修正
wheson 158c542
.cpp -> .h
wheson 5b2e627
#pragma once
wheson d18f380
有向か無向かの指定を消した
wheson 1b5fe2d
explicit
wheson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #pragma once | ||
| #include "graph.h" | ||
| #include "template.h" | ||
|
|
||
| template <typename T> | ||
| class Dijkstra { | ||
| private: | ||
| bool isDir = false; // 無向グラフ: false, 有向グラフ: true | ||
| T INF = numeric_limits<T>::max() / 10; | ||
| int V; // 頂点数 | ||
| AdjList<T> adj; // adj[始点][動的配列で始点から伸びる枝] | ||
| vector<int> prever; | ||
|
|
||
| public: | ||
| Dijkstra(int n, bool dir); | ||
| vector<T> cost; | ||
| void AddEdge(int f, int t, int c); | ||
| bool HasPath(int t); // tに至るパスはあるか | ||
| vector<int> GetShortestPath(int t); // tへの最短路 | ||
| void Run(int f); | ||
| }; | ||
|
|
||
| template <typename T> | ||
| Dijkstra<T>::Dijkstra(int n, bool dir) | ||
| : isDir(dir), V(n + 1), adj(V), prever(vector<int>(V, -1)), cost(V) { | ||
| fill(cost.begin(), cost.end(), INF); | ||
| } | ||
|
|
||
| template <typename T> | ||
| void Dijkstra<T>::AddEdge(int f, int t, int c) { | ||
| adj[f].push_back(Edge<T>(t, c)); | ||
| if (!isDir) adj[t].push_back(Edge<T>(f, c)); | ||
| } | ||
|
|
||
| template <typename T> | ||
| bool Dijkstra<T>::HasPath(int t) { | ||
| return cost[t] != INF; | ||
| } | ||
|
|
||
| template <typename T> | ||
| vector<int> Dijkstra<T>::GetShortestPath(int t) { | ||
| vector<int> path; | ||
| for (; t != -1; t = prever[t]) path.push_back(t); | ||
|
|
||
| reverse(path.begin(), path.end()); | ||
| return path; | ||
| } | ||
|
|
||
| template <typename T> | ||
| void Dijkstra<T>::Run(int firstNode) { | ||
| using Pi = pair<T, int>; | ||
| priority_queue<Pi, vector<Pi>, greater<Pi>> pq; | ||
|
|
||
| cost[firstNode] = 0; | ||
| pq.push(Pi(cost[firstNode], firstNode)); | ||
|
|
||
| while (!pq.empty()) { | ||
| Pi currentEdge = pq.top(); | ||
| pq.pop(); | ||
| if (cost[currentEdge.second] < currentEdge.first) continue; | ||
|
|
||
| for (Edge<T> tmp : adj[currentEdge.second]) { | ||
| T sumCost = currentEdge.first + tmp.cost; | ||
| if (cost[tmp.to] > sumCost) { | ||
| cost[tmp.to] = sumCost; | ||
| prever[tmp.to] = currentEdge.second; | ||
| pq.push(Pi(cost[tmp.to], tmp.to)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
他のグラフアルゴリズムのクラスでは、基本的に有向グラフを構築する仕様になっています
ダイクストラだけコンストラクタで、有向グラフかどうかを指定するのは違和感があります
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど.修正します.