Advertisement
Derga

Untitled

May 29th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. struct IdxCost{
  8. int idx;
  9. int cost;
  10. }
  11.  
  12. int main() {
  13. int towns_count, cables_count;
  14. cin >> towns_count >> cables_count;
  15. vector <vector<IdxCost>> cables(1 + towns_count);
  16. for (int i = 0; i < cables_count; ++i) {
  17. int u, v, cost;
  18. cin >> u >> v >> cost;
  19. cables[u].emplace_back(v, cost);
  20. }
  21.  
  22.  
  23. Dsu dsu(1+ towns_count);
  24. vector<int> required_cables_idxs;
  25. int total_cost = 0;
  26. int required_cabels_count = 0;
  27. for (const auto[u, v, cost, idx] : cables) {
  28. if (dsu.GetParent(u) != dsu.GetParent(v)) {
  29. dsu.Union(u, v);
  30. total_cost += cost;
  31. ++required_cabels_count;
  32. required_cables_idxs.push_back(idx);
  33. }
  34. }
  35.  
  36. sort(begin(required_cables_idxs), end(required_cables_idxs));
  37.  
  38. cout << total_cost << ' ' << required_cabels_count << '\n';
  39. for (int idx : required_cables_idxs) cout << idx << ' ';
  40.  
  41. return 0;
  42. }
  43.  
  44. /*
  45. test1
  46. 2 2
  47. 1 2 3
  48. 1 2 4
  49.  
  50. 3 1
  51. 1
  52.  
  53. test2
  54. 3 3
  55. 1 2 5
  56. 1 3 10
  57. 3 2 4
  58.  
  59. 14 2
  60. 2 3
  61. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement