999ms

Untitled

Apr 9th, 2020
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1.  
  2.  
  3. struct Edge {
  4.     int to, w; // w = {0, 1}
  5. };
  6.  
  7. const int N = 100100;
  8. vector<Edge> g[N];
  9.  
  10. vector<int> Dijkstra(int from, int n) {
  11.     vector<int> depth(n, n);
  12.     depth[from] = 0;
  13.     vector<bool> used(n);
  14.    
  15.     auto connect = [&] (int start) {
  16.         queue<int> q;
  17.         q.push(start);
  18.         vector<int> result;
  19.         result.push_back(start);
  20.         used[start] = true;
  21.         while (q.size()) {
  22.             int v = q.front();
  23.             q.pop();
  24.            
  25.             for (const auto& [to, w] : g[v]) {
  26.                 if (w != 0) continue;
  27.                 if (used[to] == true) continue;
  28.                 used[to] = true;
  29.                 result.push_back(to);
  30.                 depth[to] = depth[start];
  31.                 q.push(to);
  32.             }
  33.         }    
  34.         return result;
  35.     };
  36.    
  37.     queue<int> q;
  38.     q.push(from);
  39.     while (q.size()) {
  40.         int v = q.front();
  41.         q.pop();
  42.        
  43.         auto ne = connect(v);
  44.         for (int currentVertex : ne) {
  45.             for (const auto& [to, w] : g[currentVertex]) {
  46.                 if (depth[to] > depth[currentVertex] + w) {
  47.                     depth[to] = depth[currentVertex] + w;
  48.                     q.push(to);
  49.                 }
  50.             }
  51.         }
  52.     }
  53.     return depth;
  54. }
Add Comment
Please, Sign In to add comment