Advertisement
apl-mhd

MyfirstDijkstraCode

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