Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. struct edge {
  2.     ll to, cost;
  3.     bool operator< (const edge& other) const {
  4.         return cost > other.cost;
  5.     }
  6. };
  7.  
  8. ll dist[N];
  9. vector<edge> G[N];
  10.  
  11. void dijkstra(int src) {
  12.     memset(dist, 0x7F, sizeof(dist));
  13.     dist[src] = 0;
  14.     priority_queue<edge> minn;
  15.     minn.push({src, 0});
  16.     while (!minn.empty()) {
  17.         auto r = minn.top();
  18.         minn.pop();
  19.         ll u = r.to, cost = r.cost;
  20.  
  21.         if (dist[u] < cost) continue;
  22.  
  23.         for (edge& e : G[u]) {
  24.             ll to = e.to, c = e.cost;
  25.             if (dist[to] > dist[u] + c) {
  26.                 dist[to] = dist[u] + c;
  27.                 minn.push({to, dist[to]});
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33. void clear() {
  34.     for (int i = 0; i < n; ++i) G[i].clear();
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement