Advertisement
sukesh2000

CSES - Shortest Routes II

Mar 23rd, 2021 (edited)
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. /**************************
  2. *                         *
  3. *    Author: Sukesh       *
  4. *                         *
  5. ***************************/
  6. #include<bits/stdc++.h>
  7. #define FIO                 ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
  8. #define int                 long long int
  9. #define INF                 (int)1e14
  10. using namespace std;
  11.  
  12. class ShortestPath{
  13.     private:
  14.         vector<pair<int,int>> paths; //assign to infinity
  15.         int dest = 0;
  16.         vector<bool> processed;
  17.  
  18.         void findShortestDijkstra(int src){
  19.             paths[src].second = 0;
  20.             priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
  21.             pq.push({0,src}); // First element of the pair represents the parent node of the current node,
  22.                               // second one represents the weight/distance of the current node from the source node
  23.  
  24.             while(!pq.empty()){
  25.                 auto dist = pq.top().first, node = pq.top().second;
  26.                 pq.pop();
  27.  
  28.                 if(node==dest)
  29.                     return;
  30.  
  31.                 if(processed[node])
  32.                     continue;
  33.                 processed[node] = 1;
  34.  
  35.                 for(auto x: weighted[node]){
  36.                     int node1 = x.first, wt = x.second;
  37.  
  38.                     if(dist+wt<paths[node1].second){
  39.                         paths[node1].second = dist+wt;
  40.                         paths[node1].first = node;
  41.                         pq.push({paths[node1].second, node1});
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.  
  47.     public:
  48.         vector<vector<pair<int,int>>> weighted;
  49.         int dijkstra(int src, int end){
  50.             int n = weighted.size()-1;
  51.  
  52.             paths.clear(), paths.resize(n+1, pair<int,int> ({0,INF}));
  53.             processed.clear(), processed.resize(n+1);
  54.  
  55.             dest = end;
  56.             findShortestDijkstra(src);
  57.             if(paths[end].second>=INF){
  58.                 return -1;
  59.             }
  60.  
  61.             return paths[end].second;
  62.         }
  63. };
  64.  
  65. vector<vector<pair<int,int>>> buildAdjList(int n, int e){
  66.     vector<vector<pair<int,int>>> adj(n+1);
  67.     for(int i = 0; i<e;i++){
  68.         int u, v, w;
  69.         cin >> u >> v >> w;
  70.         adj[u].push_back({v,w});
  71.         adj[v].push_back({u,w});
  72.     }
  73.     return adj;
  74. }
  75.  
  76. void solve(){
  77.     FIO;
  78.     int n, e, q; cin >> n >> e >> q;
  79.     ShortestPath s;
  80.     s.weighted = buildAdjList(n,e);
  81.     while(q--){
  82.         int src, dest; cin >> src >> dest;
  83.         cout << s.dijkstra(src,dest);
  84.         cout << "\n";
  85.     }
  86.     return;
  87. }
  88.  
  89. signed main(){
  90.     FIO;
  91.    
  92.     #ifndef ONLINE_JUDGE
  93.         freopen("in.txt","r",stdin);
  94.         freopen("out.txt","w",stdout);
  95.     #endif
  96.  
  97.     int t=1;
  98.     // cin>>t;
  99.     while(t--)
  100.         solve();
  101.    
  102.     #ifndef ONLINE_JUDGE
  103.         cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
  104.     #endif
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement