Advertisement
Guest User

Prims

a guest
Mar 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. /**
  2. * E:\Dropbox\Profession\SUST\CSE_138_2017\Prims.cpp
  3. * Created on: 2017-11-22-15.45.44, Wednesday
  4. * Verdict: Not Solved
  5. * Author: Enamul Hassan
  6. **/
  7.  
  8. #include <bits/stdc++.h>
  9. #define _ ios_base::sync_with_stdio(0);cin.tie(0);
  10.  
  11. #define SZ(a) ((ll)a.size())
  12. #define sz 200005
  13. #define pb push_back
  14. #define pp pop_back()
  15. #define all(a) a.begin(),a.end()
  16. #define ll long long
  17. #define cntbit(mask) __builtin_popcount(mask)
  18. #define unify(a) stable_sort(a.begin(),a.end());a.resize(distance(a.begin(),unique(all(a))));
  19. #define fread freopen("input.txt","r",stdin)
  20. #define fwrite freopen("output.txt","w",stdout)
  21. #define inf (1e18)
  22. #define chng(a,b) a^=b^=a^=b;
  23. #define clr(abc,z) memset(abc,z,sizeof(abc))
  24. #define PI acos(-1)
  25. #define pi 3.14159265358979323846264338327950288419716939937510
  26. #define fr(i,a,b) for(i=a;i<=b;i++)
  27. #define cspf printf("Case %d:", cas++);
  28. #define csco cout<<"Case "<<cas++<<": ";
  29. #define mod 1000000007
  30.  
  31. #ifdef ENAM
  32. #define deb(args...) {dbg,args; cerr<<endl;}
  33. #else
  34. #define deb(args...)
  35. #endif
  36. using namespace std;
  37.  
  38. struct _deb{
  39.     template<typename T> _deb& operator , (const T& _temp){
  40.         cerr<<_temp<<" ";
  41.         return *this;
  42.     }
  43. }dbg;
  44.  
  45. /// debug template credit: Bidhan Roy
  46.  
  47. ll bigmod(ll sonkha,ll ghat,ll vag_const){ll vag_shesh=1;while(ghat>0){if(ghat%2==1){vag_shesh=(vag_shesh*sonkha)%vag_const;}ghat/=2;sonkha=(sonkha*sonkha)%vag_const;}return vag_shesh;}
  48. ll inverse_mod(ll bivajok, ll vag_const){return bigmod(bivajok,vag_const-2, vag_const);}
  49.  
  50. vector<int>adj[sz],cost[sz];
  51. int par[sz], col[sz];
  52.  
  53. priority_queue< pair<int, pair<int,int> > ,
  54. vector< pair<int, pair<int,int> > >,
  55. greater< pair<int,pair<int,int> > > >pq;
  56.  
  57. int prims(int st)
  58. {
  59.     for (int i = 0; i<adj[st].size(); i++)
  60.         pq.push({cost[st][i], {st,adj[st][i]}});
  61.     vector< pair<int, pair<int,int>  > > ans;
  62.     col[st]=1;
  63.  
  64.     int c, u, v, tot=0;
  65.     while(!pq.empty())
  66.     {
  67.         c = pq.top().first;
  68.         u = pq.top().second.first;
  69.         v = pq.top().second.second;
  70.         pq.pop();
  71.         if(!col[u] || !col[v])
  72.         {
  73.             ans.pb( {c, {u,v} } );
  74.             tot+=c;
  75.             u = col[u]?v:u;
  76.             col[u]=1;
  77.             for (int i = 0; i<adj[u].size(); i++)
  78.             {
  79.                 if(! col[ adj[u][i] ])
  80.                     pq.push({cost[u][i],
  81.                             {u,adj[u][i]} });
  82.             }
  83.         }
  84.     }
  85.  
  86.     for (int i = 0; i<ans.size(); i++)
  87.     {
  88.         cout<<i<<":"<<ans[i].first<<" -> ("
  89.         <<ans[i].second.first << "," <<
  90.         ans[i].second.second << ")\n";
  91.     }
  92.     return tot;
  93. }
  94. int main()
  95. {
  96.     _
  97.     int cas = 1,st,n,m,x,y,z;
  98.  
  99.  
  100.     cin>>n>>m;
  101.  
  102.     for (int i = 0; i<m; i++)
  103.     {
  104.         cin>>x>>y>>z;
  105.         adj[x].pb(y);
  106.         cost[x].pb(z);
  107.         adj[y].pb(x);
  108.         cost[y].pb(z);
  109.     }
  110.  
  111.     cin>>st;
  112.  
  113.     cout<<"Total Cost of MST = "<<prims(st)<<"\n";
  114.  
  115.  
  116. //    end = clock();
  117. //    time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  118. //    cerr<<"Time spent = "<<time_spent<<endl;
  119.  
  120.    return 0;
  121. }
  122. /**
  123. 6 8
  124. 1 2 1
  125. 1 3 1
  126. 1 5 1
  127. 2 4 3
  128. 2 5 2
  129. 3 6 2
  130. 4 6 3
  131. 5 6 5
  132. 1
  133. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement