Advertisement
apl-mhd

dijsktraCF

Jul 9th, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <set>
  6. #include <functional>
  7. #include <queue>
  8. #include <utility>
  9. #include <list>
  10. #include <climits>
  11. #define P printf("\n");
  12. /*
  13.     (1)----------w=10----------(2)
  14.      |                       /   |
  15.      |              _______/     |
  16.     w=1___________/             w=20
  17.      |/                         |
  18.      (3)--------- w=2 ----------(4)
  19.  
  20.  
  21.         graph v(1,2,3,4)
  22.        
  23.  
  24. */
  25.  
  26. using namespace std;
  27.  
  28. typedef pair<int, int>PAIR;
  29. int path[100]={0};
  30.  
  31. void printPath(int n){
  32.    
  33.     if(path[n]==0)
  34.         return;
  35.    
  36.     else
  37.        
  38.         printPath(path[n]);
  39.            
  40.     cout<<" "<<n;
  41.    
  42.     }
  43.  
  44. int main(int argc, char **argv)
  45. {
  46.    
  47.    
  48.     //cout<<UINT_MAX;
  49.     /*
  50.     list<int>y;
  51.     list<int>::iterator iit;
  52.     y.push_back(1);
  53.     y.push_back(2);
  54.     y.push_back(3);
  55.     y.push_back(4);
  56.    
  57.     */
  58.    
  59.    
  60.     int n,m,u,v,w;
  61.    
  62.     //cout<<"input number of bvertices and number of edges\n";
  63.    
  64.     cin>>n>>m;
  65.    
  66.     if(m==0)
  67.        
  68.         cout<<-1;
  69.    
  70.     else{
  71.    
  72.    
  73.     list<PAIR>x[n+1];
  74.    
  75.     vector<int>dist(n+1,1000010);
  76.    
  77.     list<PAIR>::iterator it;
  78.    
  79.     priority_queue<PAIR, vector<PAIR>, greater<PAIR>>q;
  80.    
  81.  
  82.    
  83.    
  84.     for(int i=0; i<m; i++){
  85.        
  86.         cin>>u>>v>>w;
  87.        
  88.         x[u].push_back(make_pair(w,v));
  89.         x[v].push_back(make_pair(w,u));
  90.                
  91.         }
  92.        
  93.     q.push(make_pair(0,1));
  94.    
  95.     while(!q.empty()){
  96.        
  97.     //  w=q.top().first;
  98.    
  99.         dist[1]=0;
  100.         path[1]=0;
  101.         u=q.top().second;
  102.         q.pop();
  103.        
  104.         for(it=x[u].begin(); it!=x[u].end(); it++){
  105.            
  106.            
  107.             w = (*it).first;
  108.             v = (*it).second;
  109.            
  110.             if(dist[v] > dist[u]+w){
  111.                
  112.                 dist[v] = dist[u]+w;
  113.                 q.push(make_pair(dist[v], v));
  114.                
  115.                 path[v]=u;
  116.                
  117.                 }
  118.                
  119.                
  120.            
  121.             }
  122.        
  123.         }
  124.        
  125.  
  126.     cout<<"1"; 
  127.     printPath(n);
  128. }
  129.  
  130. /*
  131.     cout<<"vertices->      edges \n";
  132.     for(int i=1; i<=n; i++){
  133.        
  134.         cout<<i<<" "<<dist[i]<<" \n";      
  135.        
  136.         }
  137.        
  138.         P;P;
  139.  
  140.  
  141.        
  142.        
  143.         for(int i=1; i<10; i++)
  144.        
  145.             cout<<i<<" "<<path[i]<<"\n";
  146.         */
  147.        
  148.        
  149.  
  150.  
  151. /*     
  152.        
  153.     for(int i=1;i<=n;i++){
  154.        
  155.         for(it=x[i].begin(); it !=x[i].end(); it++){
  156.            
  157.             cout<<i<<" "<<(*it).first<<" "<<(*it).second<<"\n";
  158.        
  159.            
  160.             }
  161.            
  162.         }
  163.  
  164. */
  165.        
  166.  
  167.    
  168.    
  169.    
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement