Advertisement
apl-mhd

second shortest path :)

Sep 29th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. /*lots of hard work :)*/
  2. //code is sexy
  3. #include <iostream>
  4. #include <vector>
  5. #include <map>
  6. #include <queue>
  7. #include <set>
  8. #include <utility>
  9. #include <functional>
  10. #include <list>
  11. #include <climits>
  12. #include <algorithm>
  13. #include <cstdio>
  14.  
  15. using namespace std;
  16.  
  17. #define N printf("\n");
  18.  
  19. typedef  pair<int, int>PAIR;
  20. int main() {
  21.  
  22.     priority_queue<PAIR,vector<PAIR>, greater<PAIR>>q;
  23.  
  24.     int n,e,u,v,w;
  25.  
  26.     cin>>n>>e;
  27.  
  28.     list<PAIR>graph[n+1];
  29.  
  30.     list<PAIR>::iterator it;
  31.  
  32.     for(int i=0; i<e; i++){
  33.  
  34.         cin>>u>>v>>w;
  35.  
  36.         graph[u].push_back(make_pair(w,v));
  37.         graph[v].push_back(make_pair(w,u));
  38.     }
  39.  
  40.     int dist[n];
  41.     vector<int> result;
  42.     bool visited[n];
  43.  
  44.  
  45.  
  46.     for(int i=0; i<=n; i++){
  47.  
  48.         dist[i]= INT_MAX;
  49.         visited[i]= false;
  50.  
  51.  
  52.     }
  53.  
  54.     dist[1]=0;
  55.  
  56.  
  57.     q.push(make_pair(0,1));
  58.  
  59.  
  60.  
  61.     while (!q.empty()){
  62.  
  63.         int w = q.top().first;
  64.         int edge =q.top().second;
  65.         q.pop();
  66.  
  67.         visited[edge]= true;
  68.  
  69.         for(it = graph[edge].begin(); it !=graph[edge].end(); it++ ){
  70.  
  71.             if(it->first < dist[it->second] && visited[it->second] ==false){
  72.  
  73.  
  74.  
  75.                 if(it->second == n)
  76.                     result.push_back(it->first+dist[edge]);
  77.                 else {
  78.                     dist[it->second] = it->first + dist[edge];
  79.                     q.push(make_pair(it->first, it->second));
  80.                 }
  81.  
  82.             }
  83.             ////cout<<i<<" "<<it->second<<" "<<it->first<<endl;
  84.  
  85.         }
  86.  
  87.     }
  88.  
  89.  
  90.  
  91.     sort(result.begin(), result.end());
  92.     cout<<result[1]<<endl;
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement