Advertisement
a53

Oracol

a53
Oct 17th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <limits>
  4. #include <iterator>
  5. #include <numeric>
  6. using namespace std;
  7.  
  8. int64_t SpanningTree(const vector<vector<int>>& cost) {
  9. int n = static_cast<int>(cost.size());
  10. vector<int> prim_cost(n);
  11. vector<int> available(n);
  12. iota(available.begin(), available.end(), 0);
  13. fill(next(prim_cost.begin()), prim_cost.end(),
  14. numeric_limits<int>::max());
  15. int64_t total = 0;
  16. for (int i = 0; i < n; ++i) {
  17. int best_node = 0;
  18. for (int j = 1; j < static_cast<int>(available.size()); ++j) {
  19. if (prim_cost[available[j]] < prim_cost[available[best_node]]) {
  20. best_node = j;
  21. }
  22. }
  23.  
  24. int extracted_node = available[best_node];
  25. total += prim_cost[extracted_node];
  26.  
  27. swap(available[best_node], available.back());
  28. available.pop_back();
  29.  
  30. for (auto&& node: available) {
  31. if (cost[extracted_node][node] < prim_cost[node]) {
  32. prim_cost[node] = cost[extracted_node][node];
  33. }
  34. }
  35. }
  36. return total;
  37. }
  38.  
  39. int main()
  40. {
  41. ifstream fin("oracol.in");
  42. int n; fin >> n;
  43. vector<vector<int>> cost(n + 1, vector<int>(n + 1));
  44. for (int i = 1; i <= n; ++i) {
  45. for (int j = i; j <= n; ++j) {
  46. fin >> cost[i - 1][j];
  47. cost[j][i - 1] = cost[i - 1][j];
  48. }
  49. }
  50. ofstream("oracol.out") << SpanningTree(cost) << endl;
  51. return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement