frp

Dijkstra

frp
Mar 3rd, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. vector<vector<int> > g;
  2. vector<vector<int> > w;
  3. vector<int> d;
  4. vector<int> p;
  5. vector<bool> c;
  6. int n;
  7. const int inf = 2000000000;
  8. typedef pair<int,int> q_e;
  9. void dijkstra(int s)
  10. {
  11.     d.resize(n, inf);
  12.     p.resize(n, -1);
  13.     c.resize(n);
  14.     priority_queue<q_e, vector<q_e>, greater<q_e> > q;
  15.     d[s] = 0;
  16.     q.push(make_pair(0, s));
  17.     while(!q.empty())
  18.     {
  19.         int v = q.top().second;q.pop();
  20.         if(c[v])continue;
  21.         c[v] = true;
  22.         for(int i = 0; i < g[v].size(); i++)
  23.         {
  24.             int tv = g[v][i], tw = w[v][i];
  25.             if(d[v] + tw < d[tv])
  26.             {
  27.                 d[tv] = d[v] + tw;
  28.                 p[tv] = v;
  29.                 q.push(make_pair(d[tv], tv));
  30.             }
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment