Advertisement
Guest User

dijkstra

a guest
Mar 18th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. struct no {
  2.     int v, qtd;
  3.     no() {}
  4.     no(int v, int qtd) : v(v), qtd(qtd) {}
  5.     bool operator < (const no &outro) const {
  6.         return qtd > outro.qtd;
  7.     }
  8. };
  9.  
  10. vector<int> g[maxn];
  11. int atiradores[maxn];
  12.  
  13. int dijkstra(int s, int t) {
  14.     int qtd[maxn]; memset(qtd, inf, sizeof qtd);
  15.     priority_queue<no> pq;
  16.     pq.push(no(s, atiradores[s]));
  17.     qtd[s] = atiradores[s];
  18.  
  19.     while (!pq.empty()) {
  20.         no tmp = pq.top(); pq.pop();
  21.         int u = tmp.v;
  22.         if (u == t) return qtd[u];
  23.         for (int i = 0; i < g[u].size(); i++) {
  24.             int v = g[u][i];
  25.             if (qtd[v] > qtd[u] + atiradores[v]) {
  26.                 qtd[v] = qtd[u] + atiradores[v];
  27.                 pq.push(no(v, qtd[v]));
  28.             }
  29.         }
  30.     }
  31.  
  32.     return inf;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement