Advertisement
Josif_tepe

Untitled

Sep 12th, 2023
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <queue>
  2. #include <iostream>
  3. #include <vector>
  4. #include <cstring>
  5. using namespace std;
  6. const int maxn = 2e5 + 10;
  7. const int INF = 2e9;
  8. struct node {
  9.     int idx, cost;
  10.     node(){}
  11.     node(int _idx, int _cost) {
  12.         idx = _idx;
  13.         cost = _cost;
  14.     }
  15.     bool operator < (const node & tmp) const {
  16.         return cost > tmp.cost;
  17.     }
  18. };
  19. vector<pair<int, int> > graph[maxn];
  20. void dijkstra(int S, int E) {
  21.     vector<bool> visited(maxn, false);
  22.     vector<int> distance(maxn, INF);
  23.    
  24.     distance[S] = 0;
  25.     priority_queue<node> pq;
  26.     pq.push(node(S, 0));
  27.    
  28.     while(!pq.empty()) {
  29.         node c = pq.top();
  30.         pq.pop();
  31.        
  32.         if(visited[c.idx]) {
  33.             continue;
  34.         }
  35.         visited[c.idx] = true;
  36.         for(int i = 0; i < (int) graph[c.idx].size(); i++) {
  37.             int neighbour = graph[c.idx][i].first;
  38.             int weight = graph[c.idx][i].second;
  39.             if(!visited[neighbour] and c.cost + weight < distance[neighbour]) {
  40.                 pq.push(node(neighbour, c.cost + weight));
  41.                 distance[neighbour] = c.cost + weight;
  42.             }
  43.         }
  44.     }
  45.     cout << distance[E] << endl;
  46.    
  47. }
  48. int main(int argc, const char * argv[]) {
  49.    
  50.     return 0;
  51. }
  52.  
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement