Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- #define vec vector
- #include <numeric>
- #include <functional>
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x+1<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- void cvb(vector <bool> v){
- for (bool x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvs(vector <string> v){
- for (auto a: v){
- cout<<a<<"\n";
- }
- }
- int t,n,m;
- string bin(int x){
- string r = "";
- while (x > 0){
- r += x % 2 + '0';
- x /= 2;
- }
- reverse(r.begin(), r.end());
- while (r.size() < n){
- r = '0' + r;
- }
- return r;
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin>>t;
- for (int act=0;act<t;++act){
- cin>>n>>m;
- vector <int> lock(n);
- for (int i = 0 ; i< n;++i) cin>>lock[i];
- vector <vector <int> > box(m, vector <int> (n));
- for (int i = 0 ; i < m;++i){
- for (int j=0;j<n;++j) cin>>box[i][j];
- }
- //все варианты раскрытия коробочек
- int S = 2e9;
- vector <int> ans(m);
- for (int var = 0; var < (1 << n); ++var){
- int Svar=0;
- vector <int> ansVar(m);
- vector <int> cost(m, 2e9);
- vector <bool> buy(m, 0);
- string open = bin(var);
- for (int i=0;i<n;++i){
- if (open[i] == '1'){
- Svar += lock[i];
- }
- }
- for (int good = 0; good < m; ++good){
- pii bst = {2e9, -1};
- for (int shop = 0; shop < n; ++shop){
- if (open[shop] == '0') continue;
- if (cost[good] > box[good][shop]){
- cost[good] = box[good][shop];
- ansVar[good] = shop;
- buy[good]=1;
- }
- }
- }
- Svar += accumulate(cost.begin(), cost.end(), 0);
- bool ok=1;
- for (int i = 0 ; i<m;++i){
- if (!buy[i]){
- ok=0;
- }
- }
- if (ok && Svar < S){
- S = Svar;
- ans = ansVar;
- }
- }
- cout<<S<<"\n";
- cv(ans);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement