Advertisement
Sander447

prim.cpp

Jun 19th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. // Author: Sander de Beet
  2.  
  3. #include <bits/stdc++.h>  // This will work only for g++ compiler.
  4.  
  5. #define f0r(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
  6. #define f1r(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
  7. #define f0rr(i, n) for (int i = (int)(n) - 1; i >= 0; --i) // reverse 0 based.
  8. #define f1rr(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
  9.  
  10. // short hand for usual tokens
  11. #define pb push_back
  12. #define fi first
  13. #define se second
  14.  
  15. // to be used with algorithms that processes a container Eg: find(all(c),42)
  16. #define all(x) (x).begin(), (x).end() //Forward traversal
  17. #define rall(x) (x).rbegin, (x).rend() //reverse traversal
  18.  
  19. // find if a given value is present in a container. Container version. Runs in log(n) for set and map
  20. #define present(c,x) ((c).find(x) != (c).end())
  21.  
  22. // find version works for all containers. This is present in std namespace.
  23. #define cpresent(c,x) (find(all(c),x) != (c).end())
  24.  
  25. #define uni(x) (x).erase(unique(all(x)), (x).end())
  26. #define debug(x) cout<<#x<<" -> "<<x<<'\n'
  27.  
  28. using namespace std;
  29.  
  30. // Shorthand for commonly used types
  31. typedef vector<int> vi;
  32. typedef vector<vi> vvi;
  33. typedef vector<string> vs;
  34. typedef pair<int, int> ii;
  35. typedef vector<ii> vii;
  36. typedef long long ll;
  37. typedef vector<ll> vll;
  38. typedef vector<vll> vvll;
  39. typedef long double ld;
  40.  
  41.  
  42. void fast_IO() {
  43.     ios::sync_with_stdio(false);
  44.     cin.tie(NULL); cout.tie(NULL);
  45. }
  46.  
  47. void solve() {
  48.     int n, m; cin >> n >> m;
  49.     int mst_cost = 0;
  50.  
  51.     vector<pair<int, int>> adj[n];
  52.     vector<int> vis(n, 0);
  53.     vector<int> key(n, INT_MAX);
  54.     vector<int> parent(n, -1);
  55.  
  56.     for (int i = 0; i < m; i++) {
  57.         int a, b, c;
  58.         cin >> a >> b >> c;
  59.         adj[a].push_back({c, b});
  60.         adj[b].push_back({c, a});
  61.     }
  62.  
  63.     priority_queue<ii, vector<ii>, greater<ii>> pq;
  64.  
  65.     // {cost, node}
  66.     pq.push({0LL, 0});
  67.  
  68.     while (!pq.empty()) {
  69.         ii item = pq.top(); pq.pop();
  70.  
  71.         int cost = item.first;
  72.         int node = item.second;
  73.  
  74.         if (!vis[node]) {
  75.             mst_cost += cost;
  76.             vis[node] = 1;
  77.  
  78.             for (auto npair : adj[node]) {
  79.                 int weight = npair.fi;
  80.                 int nb = npair.second;
  81.  
  82.                 if (!vis[nb] && weight < key[nb]) {
  83.                     key[nb] = weight;
  84.                     pq.push(npair);
  85.                     parent[nb] = node;
  86.                 }
  87.             }
  88.         }
  89.     }
  90.  
  91.     for (int i = 0; i < n; ++i) {
  92.         if (parent[i] != -1) {
  93.             cout << parent[i] << " " << i << "\n";
  94.         }
  95.     }
  96.  
  97.     cout << mst_cost << "\n";
  98. }
  99.  
  100.  
  101. int main(int argc, char* argv[]) {
  102.     fast_IO();
  103.     auto a = freopen(argv[1], "r", stdin);
  104.  
  105.     solve();
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement