Last active
October 20, 2022 11:46
-
-
Save hucancode/fc2c32b5fcb22093450a68d3993b8f04 to your computer and use it in GitHub Desktop.
quick implementation of dijkstra
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
typedef long long ll; | |
typedef vector<int> vi; | |
typedef vector<vector<int>> vvi; | |
typedef vector<ll> vl; | |
typedef vector<vl> vvl; | |
typedef pair<ll, int> pli; | |
vl dijkstra(int n, map<int, vector<pli>>& adj, int start) { | |
vl dist(n, -1); | |
dist[start] = 0; | |
priority_queue<pli, vector<pli>, greater<pli>> vis; | |
vis.emplace(0, start); | |
vector<bool> checked(n, false); | |
while(!vis.empty()) { | |
ll uw; | |
int u; | |
tie(uw, u) = vis.top(); | |
vis.pop(); | |
if(checked[u]) { | |
continue; | |
} | |
checked[u] = true; | |
for(auto x: adj[u]) { | |
ll w; | |
int v; | |
tie(w, v) = x; | |
if(checked[v]) { | |
continue; | |
} | |
ll next = dist[u]+w; | |
if(dist[v] == -1 || next < dist[v]) { | |
vis.emplace(next, v); | |
dist[v] = next; | |
} | |
} | |
} | |
return dist; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment