Advertisement
apl-mhd

Minimum spanning tree prims

May 5th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <stack>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <climits>
  8. #include <utility>
  9. #include <map>
  10. #define MAX_SZ 100
  11. using namespace std;
  12.  
  13. /*
  14. 6 9
  15. 0 1 1
  16. 0 2 3
  17. 2 1 3
  18. 2 3 1
  19. 1 3 1
  20. 3 5 5
  21. 3 4 4
  22. 4 5 2
  23. 1 5 6
  24. */
  25.  
  26. /*
  27.  Input:
  28.  
  29.        1       6
  30.  (0)------(1)-------(5)
  31.   |      /  |        |
  32.   |     /   |        |
  33. 3 |   3/    | 1      | 2
  34.   |   /     |        |
  35.   |  /      |        |
  36.   | /       |        |
  37.  (2)------(3)------(4)
  38.        1        4
  39.  
  40. */
  41.  
  42.  
  43. /*
  44.  
  45. Output:
  46.  
  47.        1
  48.  (0)------(1)      (5)
  49.            |        |
  50.            |        |
  51.            | 1      |  2
  52.            |        |
  53.            |        |
  54.            |        |
  55.  (2)------(3)------(4)
  56.        1       4
  57.  
  58. */
  59.  
  60. int n,m;
  61.  
  62. int dist[MAX_SZ];
  63. int visited[MAX_SZ];
  64. int parent[MAX_SZ];
  65.  
  66. map<int, pair<int,int>> mp;
  67. vector< vector< pair<int, int> > >adj;
  68. priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>>q;
  69.  
  70.  
  71.  
  72. void print_graph(){
  73.  
  74.     printf("n : %d, %d : m\n", n, m);
  75.  
  76.     for(int i=0; i<n; i++){
  77.  
  78.         // printf("%d -> ",i);
  79.  
  80.         for(int j=0; j < adj[i].size(); j++){
  81.  
  82.  
  83.  
  84.             printf("(%d, %d ) -> %d \n",i, adj[i][j].first, adj[i][j].second);
  85.         }
  86.  
  87.         printf("\n");
  88.     }
  89.  
  90. }
  91.  
  92.  
  93. void init(int s){
  94.  
  95.     for(int i=0; i< n; i++){
  96.         dist[i] = INT_MAX;
  97.         visited[i] = 0;
  98.         parent[i] = -1;
  99.  
  100.     }
  101.  
  102.     dist[s]=0;
  103.  
  104. }
  105.  
  106.  
  107. void dijkstra(int s){
  108.  
  109.     init(s);
  110.     q.push(make_pair(0,0));
  111.  
  112.     while(!q.empty()){
  113.  
  114.         pair<int,int>p;
  115.  
  116.         p = q.top();
  117.         q.pop();
  118.  
  119.         int u = p.second;
  120.         cout<<u<<" ";
  121.  
  122.         visited[u] = 1;
  123.  
  124.  
  125.         for (int i = 0; i < adj[u].size() ; ++i) {
  126.  
  127.             int v = adj[u][i].first;
  128.             int w = adj[u][i].second;
  129.             if(visited[v] == 0 && w < dist[v]) {
  130.                 dist[v] = w;
  131.                 q.push(make_pair(w,v));
  132.                 mp[v] = make_pair(u,v);
  133.  
  134.             }
  135.         }
  136.     }
  137. }
  138.  
  139.  
  140. int main()
  141. {
  142.  
  143.     adj.resize(MAX_SZ);
  144.     freopen("graph.txt", "r", stdin);
  145.     scanf("%d%d",&n,&m);
  146.  
  147.  
  148.  
  149.     for(int i=0; i<m; i++){
  150.         int u, v,w;
  151.  
  152.         scanf("%d%d%d",&u,&v,&w);
  153.         adj[u].push_back(make_pair(v,w));
  154.         adj[v].push_back(make_pair(u,w));
  155.  
  156.     }
  157.  
  158.     //print_graph();
  159.  
  160.  
  161.     dijkstra(0);
  162.  
  163.  
  164.     cout<<endl;
  165.  
  166.     for (int j = 0; j <n ; ++j) {
  167.  
  168.         //cout<<j<<" "<<dist[j]<<endl;
  169.  
  170.         cout<<mp[j].first<<" "<<mp[j].second<<endl;
  171.  
  172.     }
  173.  
  174.  
  175.  
  176.  
  177.     return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement