Advertisement
apl-mhd

dijkstraList

Jul 9th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 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. /*
  11.     (1)----------w=10----------(2)
  12.      |                       /   |
  13.      |              _______/     |
  14.     w=1___________/             w=20
  15.      |/                         |
  16.      (3)--------- w=2 ----------(4)
  17.  
  18.  
  19.         graph v(1,2,3,4)
  20.        
  21.  
  22. */
  23.  
  24. using namespace std;
  25.  
  26. typedef pair<int, int>PAIR;
  27.  
  28. int main(int argc, char **argv)
  29. {
  30.     /*
  31.     list<int>y;
  32.     list<int>::iterator iit;
  33.     y.push_back(1);
  34.     y.push_back(2);
  35.     y.push_back(3);
  36.     y.push_back(4);
  37.    
  38.     */
  39.    
  40.    
  41.     int n,m,u,v,w;
  42.    
  43.     cout<<"input number of bvertices and number of edges\n";
  44.    
  45.     cin>>n>>m;
  46.    
  47.     list<PAIR>x[n+1];
  48.    
  49.     vector<int>dist(n+1,1000010);
  50.    
  51.     list<PAIR>::iterator it;
  52.    
  53.     priority_queue<PAIR, vector<PAIR>, greater<PAIR>>q;
  54.    
  55.  
  56.    
  57.    
  58.     for(int i=0; i<m; i++){
  59.        
  60.         cin>>u>>v>>w;
  61.        
  62.         x[u].push_back(make_pair(w,v));
  63.         x[v].push_back(make_pair(w,u));
  64.                
  65.         }
  66.        
  67.     q.push(make_pair(0,1));
  68.    
  69.     while(!q.empty()){
  70.        
  71.     //  w=q.top().first;
  72.    
  73.         dist[1]=0;
  74.         u=q.top().second;
  75.         q.pop();
  76.        
  77.         for(it=x[u].begin(); it!=x[u].end(); it++){
  78.            
  79.            
  80.             w = (*it).first;
  81.             v = (*it).second;
  82.            
  83.             if(dist[v] > dist[u]+w){
  84.                
  85.                 dist[v] = dist[u]+w;
  86.                 q.push(make_pair(dist[v], v));
  87.                
  88.                 }
  89.                
  90.                
  91.            
  92.            
  93.            
  94.             }
  95.        
  96.         }
  97.        
  98.  
  99.  
  100.     for(int i=1; i<=n; i++){
  101.        
  102.         cout<<"vertices->           edges \n";
  103.         cout<<i<<" "<<dist[i]<<" \n";
  104.        
  105.        
  106.         }
  107.  
  108.  
  109. /*     
  110.        
  111.     for(int i=1;i<=n;i++){
  112.        
  113.         for(it=x[i].begin(); it !=x[i].end(); it++){
  114.            
  115.             cout<<i<<" "<<(*it).first<<" "<<(*it).second<<"\n";
  116.        
  117.            
  118.             }
  119.            
  120.         }
  121.  
  122. */
  123.        
  124.  
  125.    
  126.    
  127.    
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement