Advertisement
Korotkodul

mosh d 2

Feb 27th, 2022 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 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.  
  68. for (int act=0;act<t;++act){
  69. cin>>n>>m;
  70. vector <int> lock(n);
  71. for (int i = 0 ; i< n;++i) cin>>lock[i];
  72. vector <vector <int> > box(m, vector <int> (n));
  73. for (int i = 0 ; i < m;++i){
  74. for (int j=0;j<n;++j) cin>>box[i][j];
  75. }
  76. //все варианты раскрытия коробочек
  77. int S = 2e9;
  78. vector <int> ans(m);
  79. for (int var = 0; var < (1 << n); ++var){
  80. int Svar=0;
  81. vector <int> ansVar(m);
  82. vector <int> cost(m, 2e9);
  83. vector <bool> buy(m, 0);
  84. string open = bin(var);
  85. for (int i=0;i<n;++i){
  86. if (open[i] == '1'){
  87. Svar += lock[i];
  88. }
  89. }
  90. for (int good = 0; good < m; ++good){
  91. pii bst = {2e9, -1};
  92. for (int shop = 0; shop < n; ++shop){
  93. if (open[shop] == '0') continue;
  94. if (cost[good] > box[good][shop]){
  95. cost[good] = box[good][shop];
  96. ansVar[good] = shop;
  97. buy[good]=1;
  98. }
  99. }
  100. }
  101. Svar += accumulate(cost.begin(), cost.end(), 0);
  102. bool ok=1;
  103. for (int i = 0 ; i<m;++i){
  104. if (!buy[i]){
  105. ok=0;
  106. }
  107. }
  108. if (ok && Svar < S){
  109. S = Svar;
  110. ans = ansVar;
  111. }
  112. }
  113. cout<<S<<"\n";
  114. cv(ans);
  115. }
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement