Advertisement
apl-mhd

minimum spaning tree prim's

Jul 20th, 2017
118
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.            
  71.             q.pop();
  72.            
  73.             if(visited[u])
  74.                 continue;
  75.            
  76.             visited[u]= true;
  77.            
  78.             spaningW +=w;
  79.            
  80.             for(it = graph[u].begin(); it != graph[u].end(); it++){
  81.                
  82.  
  83.                     if(it->first< dist[it->second]){
  84.                        
  85.                         q.push(make_pair(it->first, it->second));
  86.                        
  87.                         dist[it->second] = it->first;
  88.                        
  89.                         }
  90.                
  91.                 }
  92.            
  93.            
  94.            
  95.             }
  96.    
  97.    
  98.     P;
  99.     cout<<spaningW;
  100.  
  101.  
  102.    
  103.     /*
  104.      * 6 9
  105.      * 1 2 3
  106.      * 1 4 1
  107.      * 2 3 3
  108.      * 2 4 3
  109.      * 3 4 1
  110.      * 4 5 6
  111.      * 3 5 5
  112.      * 3 6 4
  113.      * 5 6 2
  114.      *
  115.      *
  116.      *
  117.      *
  118.      *
  119.      * */
  120.    
  121.    
  122. return 0;    
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement