summaryrefslogtreecommitdiff
path: root/graph/dijkstra.cpp
diff options
context:
space:
mode:
authorPaul Jungeblut <s_jungeb@i08pc75.atis-stud.uni-karlsruhe.de>2014-11-18 14:19:42 +0100
committerPaul Jungeblut <s_jungeb@i08pc75.atis-stud.uni-karlsruhe.de>2014-11-18 14:19:42 +0100
commitfee5322fe37cb270a93c1693e2668e53138f192d (patch)
tree0118a14e625bc47c7619df8556b9c824bd8b7760 /graph/dijkstra.cpp
parent8ceb0e35278563a3b315f04fc60fc9086d6c7a8a (diff)
dijkstra
Diffstat (limited to 'graph/dijkstra.cpp')
-rw-r--r--graph/dijkstra.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/graph/dijkstra.cpp b/graph/dijkstra.cpp
new file mode 100644
index 0000000..e7ec0c3
--- /dev/null
+++ b/graph/dijkstra.cpp
@@ -0,0 +1,20 @@
+priority_queue<ii, vector<ii>, greater<ii> > pq;
+vector<int> dist;
+dist.assign(NUM_VERTICES, INF);
+dist[0] = 0;
+pq.push(ii(0, 0));
+
+while (!pq.empty()) {
+ di front = pq.top(); pq.pop();
+ int curNode = front.second, curDist = front.first;
+
+ if (curDist > dist[curNode]) continue;
+
+ for (i = 0; i < (int)adjlist[curNode].size(); i++) {
+ int nextNode = adjlist[curNode][i].first, nextDist = curDist + adjlist[curNode][i].second;
+
+ if (nextDist < dist[nextNode]) {
+ dist[nextNode] = nextDist; pq.push(ii(nextDist, nextNode));
+ }
+ }
+} \ No newline at end of file