Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include <queue>
  4. #define INF 32000
  5. using namespace std;
  6.  
  7. vector < vector< pair<long long, long long> > > graph;
  8.  
  9. long long n, m, x, y, z;
  10.  
  11. void dijkstra(int s){
  12.     vector<long long> d(n+1, INF);                                    
  13.     d[s]=0;                                                            
  14.     priority_queue <pair<int, int> > q;                                
  15.     q.push(make_pair(s, 0));                                          
  16.     while(!q.empty()){                                                
  17.         pair<int, int> top = q.top();                                  
  18.         q.pop();                                                      
  19.         for(int i=0; i<graph[top.first].size(); i++){                  
  20.             int cur_vert=graph[top.first][i].first;                    
  21.             int cur_dist = graph[top.first][i].second;                
  22.             if(d[cur_vert]>d[top.first]+cur_dist){                    
  23.                 d[cur_vert]=d[top.first]+cur_dist;                    
  24.                 q.push(make_pair(cur_vert, d[top.first]+cur_dist));    
  25.             }
  26.         }
  27.     }
  28.  
  29.     if(d[n]==INF) cout<<-1<<endl;
  30.     else cout<<d[n]<<endl;
  31. }
  32.  
  33. int main()
  34. {
  35.     cin>>n>>m;
  36.     graph.resize(n+1);
  37.     for(int i=0; i<m; i++){
  38.         cin>>x>>y>>z;
  39.         graph[x].push_back(make_pair(y, z));
  40.     }
  41.     dijkstra(1);
  42.  
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement