Advertisement
Korotkodul

mosh d 3

Feb 27th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define vec vector
  12. #include <numeric>
  13. #include <functional>
  14. using namespace std;
  15. using ll = long long;
  16. using ld = long double;
  17. using db = double;
  18. void cv(vector <int> &v){
  19. for (auto x: v) cout<<x+1<<' ';
  20. cout<<"\n";
  21. }
  22.  
  23. void cvl(vector <ll> &v){
  24. for (auto x: v) cout<<x<<' ';
  25. cout<<"\n";
  26. }
  27.  
  28.  
  29. void cvv(vector <vector <int> > &v){
  30. for (auto x: v) cv(x);
  31. cout<<"\n";
  32. }
  33.  
  34. void cvb(vector <bool> v){
  35. for (bool x: v) cout<<x<<' ';
  36. cout<<"\n";
  37. }
  38. void cvs(vector <string> v){
  39. for (auto a: v){
  40. cout<<a<<"\n";
  41. }
  42. }
  43.  
  44. int t,n,m;
  45. string bin(int x){
  46. string r = "";
  47.  
  48. while (x > 0){
  49. r += x % 2 + '0';
  50. x /= 2;
  51. }
  52. reverse(r.begin(), r.end());
  53. while (r.size() < n){
  54. r = '0' + r;
  55.  
  56. }
  57. return r;
  58. }
  59.  
  60. int main()
  61. {
  62. ios::sync_with_stdio(0);
  63. cin.tie(0);
  64. cout.tie(0);
  65.  
  66. cin>>t;
  67. vector <vector <int> > answer(t);
  68. vector <int> Sans(t);
  69. for (int act=0;act<t;++act){
  70. cin>>n>>m;
  71. vector <int> lock(n);
  72. for (int i = 0 ; i< n;++i) cin>>lock[i];
  73. vector <vector <int> > box(m, vector <int> (n));
  74. for (int i = 0 ; i < m;++i){
  75. for (int j=0;j<n;++j) cin>>box[i][j];
  76. }
  77. //все варианты раскрытия коробочек
  78. int S = 2e9;
  79. vector <int> ans(m);
  80. for (int var = 0; var < (1 << n); ++var){
  81. int Svar=0;
  82. vector <int> ansVar(m);
  83. vector <int> cost(m, 2e9);
  84. vector <bool> buy(m, 0);
  85. string open = bin(var);
  86. for (int i=0;i<n;++i){
  87. if (open[i] == '1'){
  88. Svar += lock[i];
  89. }
  90. }
  91. for (int good = 0; good < m; ++good){
  92. pii bst = {2e9, -1};
  93. for (int shop = 0; shop < n; ++shop){
  94. if (open[shop] == '0') continue;
  95. if (cost[good] > box[good][shop]){
  96. cost[good] = box[good][shop];
  97. ansVar[good] = shop;
  98. buy[good]=1;
  99. }
  100. }
  101. }
  102. Svar += accumulate(cost.begin(), cost.end(), 0);
  103. bool ok=1;
  104. for (int i = 0 ; i<m;++i){
  105. if (!buy[i]){
  106. ok=0;
  107. }
  108. }
  109. if (ok && Svar < S){
  110. S = Svar;
  111. ans = ansVar;
  112. }
  113. }
  114. Sans[act] = S;
  115. answer[act] = ans;
  116.  
  117. }
  118. for (int i = 0; i < t;++i){
  119. cout<<Sans[i]<<"\n";
  120. cv(answer[i]);
  121. }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement