Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
cpbook-code/ch4/sssp/dijkstra.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
71 lines (59 sloc)
2.15 KB
This file contains 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
#include <bits/stdc++.h> | |
using namespace std; | |
typedef pair<int, int> ii; | |
typedef vector<int> vi; | |
typedef vector<ii> vii; | |
const int INF = 1e9; // INF = 1B, not 2^31-1 to avoid overflow | |
int main() { | |
/* | |
// Graph in Figure 4.17 | |
5 7 0 | |
0 1 2 | |
0 2 6 | |
0 3 7 | |
1 3 3 | |
1 4 6 | |
2 4 1 | |
3 4 5 | |
*/ | |
freopen("dijkstra_in.txt", "r", stdin); | |
int V, E, s; scanf("%d %d %d", &V, &E, &s); | |
vector<vii> AL(V, vii()); | |
while (E--) { | |
int u, v, w; scanf("%d %d %d", &u, &v, &w); | |
AL[u].emplace_back(v, w); // directed graph | |
} | |
vi dist(V, INF); dist[s] = 0; // INF = 1e9 here | |
// Original Dijkstra's algorithm | |
/* | |
set<ii> pq; // balanced BST version | |
for (int u = 0; u < V; ++u) // dist[u] = INF | |
pq.insert({dist[u], u}); // but dist[s] = 0 | |
// sort the pairs by non-decreasing distance from s | |
while (!pq.empty()) { // main loop | |
auto [d, u] = *pq.begin(); // shortest unvisited u | |
pq.erase(pq.begin()); | |
for (auto &[v, w] : AL[u]) { // all edges from u | |
if (dist[u]+w >= dist[v]) continue; // not improving, skip | |
pq.erase(pq.find({dist[v], v})); // erase old pair | |
dist[v] = dist[u]+w; // relax operation | |
pq.insert({dist[v], v}); // enqueue better pair | |
} | |
} | |
*/ | |
// (Modified) Dijkstra's algorithm | |
priority_queue<ii, vector<ii>, greater<ii>> pq; pq.push({0, s}); | |
// sort the pairs by non-decreasing distance from s | |
while (!pq.empty()) { // main loop | |
auto [d, u] = pq.top(); pq.pop(); // shortest unvisited u | |
if (d > dist[u]) continue; // a very important check | |
for (auto &[v, w] : AL[u]) { // all edges from u | |
if (dist[u]+w >= dist[v]) continue; // not improving, skip | |
dist[v] = dist[u]+w; // relax operation | |
pq.push({dist[v], v}); // enqueue better pair | |
} | |
} | |
for (int u = 0; u < V; ++u) | |
printf("SSSP(%d, %d) = %d\n", s, u, dist[u]); | |
return 0; | |
} |