Advertisement
apl-mhd

minimum spaning tree

Sep 26th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 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. #include <map>
  12. #define P printf("\n");
  13.  
  14.  
  15.  
  16. using namespace std;
  17. typedef pair<int, int> PAIR;
  18.  
  19. const  int INF = INT_MAX;
  20.  
  21.  
  22. int main(int argc, char **argv) {
  23.    
  24.    
  25.     int n, e, u,v, w;int spaningW=0;
  26.    
  27.     list<PAIR>:: iterator it;
  28.    
  29.     priority_queue<PAIR, vector<PAIR>, greater<PAIR>>q;
  30.    
  31.     //cout<<"code is sexy";
  32.     cin>>n>>e;
  33.  
  34.     //cout<<"code is sexy"<<n<<" "<<e;
  35.  
  36.     bool visited[100];
  37.     int dist[100];
  38.    
  39.    
  40.     for(int i=0; i<=n; i++){
  41.         dist[i] = 9999; visited[i]=false;
  42.    
  43.     }
  44.        
  45.     list<PAIR>graph[n+1];
  46.    
  47.    
  48.        for (int i = 1; i <= e; i++) {
  49.  
  50.             cin>>u>>v>>w;
  51.  
  52.             graph[u].push_back(make_pair(w, v));
  53.             graph[v].push_back(make_pair(w, u));
  54.  
  55.         }
  56.        
  57.     //cout<<"end";
  58.    
  59.        
  60.         q.push(make_pair(0,1));
  61.        
  62.        
  63.        
  64.         while(!q.empty()){
  65.            
  66.            
  67.             w = q.top().first;
  68.            
  69.             u = q.top().second;
  70.             cout<<w<<"="<<u<<""<<endl;
  71.            
  72.             q.pop();
  73.            
  74.  
  75.             if(visited[u])
  76.                 continue;
  77.             visited[u]= true;
  78.            
  79.             spaningW +=w;
  80.            
  81.             for(it = graph[u].begin(); it != graph[u].end(); it++){
  82.                
  83.                
  84.                
  85.                     if(visited[it->second]==false){
  86.                     if((it->first< dist[it->second])){
  87.                        
  88.                         q.push(make_pair(it->first, it->second));
  89.                        
  90.                         dist[it->second] = it->first;
  91.                        
  92.                         }
  93.                 }
  94.                
  95.                
  96.                
  97.                 //cout<<u<<" "<<it->second<<" "<<it->first<<endl;
  98.             }
  99.            
  100.                
  101.            
  102.            
  103.             }
  104.    
  105.    
  106.     P;
  107.     cout<<spaningW;
  108.  
  109.  
  110.    
  111.    
  112. return 0;    
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement