Advertisement
pasholnahuy

Untitled

Apr 29th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <tuple>
  6. #include <set>
  7.  
  8. using namespace std;
  9.  
  10. class DSU {
  11. public:
  12. vector<int> parent;
  13. vector<int> height;
  14.  
  15. DSU(int n) {
  16. parent.resize(n);
  17. for (int i = 0; i < n; ++i) {
  18. parent[i] = i;
  19. }
  20. height.assign(n, 0);
  21. }
  22.  
  23. int findRoot(int v) {
  24. if (v == parent[v]) {
  25. return v;
  26. }
  27. int ans = findRoot(parent[v]);
  28. parent[v] = ans;
  29. return ans;
  30. }
  31.  
  32. void Union(int v1, int v2) {
  33. if (height[v1] >= height[v2]) {
  34. parent[v2] = v1;
  35. height[v1] = max(height[v1], height[v2] + 1);
  36. } else {
  37. parent[v1] = v2;
  38. height[v2] = max(height[v2], height[v1] + 1);
  39. }
  40. }
  41.  
  42.  
  43. };
  44.  
  45. int main() {
  46. int n, m;
  47. cin >> n >> m;
  48. DSU graph(n);
  49. vector<pair<pair<int, int>, pair<int, int>>> edges;
  50. vector<int> edge_weight;
  51. for (size_t i = 0; i < m; ++i) {
  52. int v1, v2, w;
  53. cin >> v1 >> v2 >> w, --v1, --v2;
  54. edges.emplace_back(make_pair(w, i), make_pair(v1, v2));
  55. edge_weight.emplace_back(w);
  56. }
  57. int p5, q5, p6, q6;
  58. cin >> p5 >> q5 >> p6 >> q6;
  59. sort(edges.begin(), edges.end());
  60. int edge_cnt = 0;
  61. vector<int> used_edges;
  62. vector<int> pack(q5 + 1, -1);
  63. int max_possible = 0;
  64. //weight.first - weight, weight.second - index
  65. for (auto [weight, coord]: edges) {
  66. if (graph.findRoot(coord.first) != graph.findRoot(coord.second)) {
  67. graph.Union(graph.findRoot(coord.first), graph.findRoot(coord.second));
  68. ++edge_cnt;
  69. used_edges.emplace_back(weight.second);
  70. for (int i = q5; i > 0; --i) {
  71. if (pack[i] > -1 && i + weight.first <= q5) {
  72. pack[i + weight.first] = weight.second;
  73. max_possible = max(i + weight.first, max_possible);
  74. } else if (pack[i] == -1) {
  75. pack[i] = weight.second;
  76. max_possible = max(i, max_possible);
  77. }
  78. }
  79. }
  80. }
  81. if (edge_cnt < n - 1) {
  82. cout << "impossible";
  83. } else {
  84. set<int> cheap;
  85. while (max_possible > 0) {
  86. cheap.emplace(pack[max_possible]);
  87. max_possible -= edge_weight[pack[max_possible]];
  88. }
  89. int total_cost = 0;
  90. for (auto edge: used_edges) {
  91. if (!cheap.contains(edge)) {
  92. q6 -= edge_weight[edge];
  93. total_cost += edge_weight[edge] * p6;
  94. } else {
  95. total_cost += edge_weight[edge] * p5;
  96. }
  97. }
  98. if (q6 < 0){
  99. cout << "impossible";
  100. } else {
  101. cout << total_cost << '\n';
  102. for (auto edge: used_edges){
  103. if (cheap.contains(edge)){
  104. cout << edge + 1 << " 5\n";
  105. } else {
  106. cout << edge + 1 << " 6\n";
  107. }
  108. }
  109. }
  110. }
  111.  
  112. return 0;
  113. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement