Advertisement
fireLUFFY

PRIMS-MST

Oct 3rd, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. //Efficient approach of PRIMS Algorithm for finding MST
  2. //Time Complexity: O(nlogn) (used heap ds for keeping track of minimum value, brings down the
  3. //search complexity to logn)
  4.  
  5. /*
  6. input:
  7. altered input   || original input
  8. 6 9
  9. 0 1 5           ||   1 2 5
  10. 0 2 4           ||   1 3 4
  11. 0 3 6           ||   1 4 6
  12. 1 2 2           ||   2 3 2
  13. 2 3 1           ||   3 4 1
  14. 1 4 9           ||   2 5 9
  15. 2 4 3           ||   3 5 3
  16. 3 5 7           ||   4 6 7
  17. 4 5 10          ||   5 6 10
  18. */
  19.  
  20. /*
  21. output:
  22. 2 --- 1
  23. 0 --- 2
  24. 2 --- 3
  25. 2 --- 4
  26. 3 --- 5
  27.  
  28. the MST created will be:
  29.  
  30.         1
  31.         |
  32.         |4
  33.      2  |  1
  34.   2-----3-----4
  35.         |     |
  36.        3|     |7
  37.         |     |
  38.         5     6
  39.  
  40. total sum: 17
  41. */
  42.  
  43. #include<bits/stdc++.h>
  44. using namespace std;
  45.  
  46. #define int long long
  47. #define inf 2e18
  48. int MOD=1000000007;//998244353;
  49. const int nn=1000050;
  50. bool prime[nn]; //array to store precalculated primes till 10^6
  51. void cal_primes(){memset(prime,true,sizeof(prime)); for(int i=2;i<=sqrt(nn);++i){ if(prime[i]==true){ for(int j=i*i;j<=nn;j+=i){prime[j]=false;}}}}
  52.  
  53. void solve(int t)
  54. {
  55.     int testcases=t;
  56.     while(t--)
  57.     {
  58.         int n,m;cin>>n>>m;
  59.         vector<pair<int,int>> g[n];
  60.  
  61.         int x,y,wt;
  62.         for(int i=0;i<m;i++){
  63.             cin>>x>>y>>wt;
  64.             g[x].push_back({y,wt});
  65.             g[y].push_back({x,wt});
  66.         }
  67.  
  68.         int parent[n],key[n];
  69.         bool mstSet[n];
  70.  
  71.         memset(parent,-1,sizeof(parent));
  72.         memset(mstSet,false,sizeof(mstSet));
  73.  
  74.         priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
  75.  
  76.         key[0]=0,parent[0]=(-1);
  77.         pq.push({0,0});
  78.  
  79.         for(int i=0;i<n-1;i++){
  80.             int u=pq.top().second;
  81.             pq.pop();
  82.  
  83.             mstSet[u]=true;
  84.  
  85.             for(auto it:g[u]){
  86.                 int v=it.first;
  87.                 int weight=it.second;
  88.                 if(!mstSet[v] && weight<key[v]){
  89.                     parent[v]=u;
  90.                     pq.push({key[v],v});
  91.                     key[v]=weight;
  92.                 }
  93.             }
  94.         }
  95.  
  96.         for(int i=1;i<n;i++)
  97.             cout<<parent[i]<<" -- "<<i<<endl;
  98.     }
  99. }
  100.  
  101. main()
  102. {
  103.     auto start=chrono::system_clock::now();
  104.     {
  105.         #ifndef ONLINE_JUDGE
  106.             freopen("input.txt","r",stdin);
  107.             freopen("output.txt","w",stdout);
  108.             freopen("error.txt","w",stderr);
  109.         #endif 
  110.         ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  111.         int t=1;
  112.     //  cin>>t;
  113.         solve(t);
  114.     }
  115.     auto end=chrono::system_clock::now();
  116.     chrono::duration<double> elapsed=end-start;
  117. //  cout<<endl<<"Time taken: "<<elapsed.count()<<" sec";
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement