|
| 1 | +// 비트마스킹 |
| 2 | +#include <bits/stdc++.h> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +int N, mp, mf, ms, mv; |
| 6 | +int p[16], f[16], s[16], v[16], cost[16]; |
| 7 | +int minRes = 1e9; |
| 8 | + |
| 9 | +vector <int> resIdx; |
| 10 | + |
| 11 | +int main() { |
| 12 | + ios_base::sync_with_stdio(false); |
| 13 | + cin.tie(NULL); cout.tie(NULL); |
| 14 | + |
| 15 | + cin >> N; |
| 16 | + cin >> mp >> mf >> ms >> mv; |
| 17 | + for(int i = 0; i < N; i++){ |
| 18 | + cin >> p[i] >> f[i] >> s[i] >> v[i] >> cost[i]; |
| 19 | + } |
| 20 | + |
| 21 | + // 모든 부분 집합, 비트마스킹 |
| 22 | + for(int i = 1; i < (1 << N); i++){ |
| 23 | + int psum = 0, fsum = 0, ssum = 0, vsum = 0, costsum = 0; |
| 24 | + vector <int> idx; |
| 25 | + |
| 26 | + for(int j = 0; j < N; j++){ |
| 27 | + if (i & (1 << j)) { // j번째 식재료가 포함된 경우 |
| 28 | + psum += p[j]; |
| 29 | + fsum += f[j]; |
| 30 | + ssum += s[j]; |
| 31 | + vsum += v[j]; |
| 32 | + costsum += cost[j]; |
| 33 | + idx.push_back(j + 1); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + // 필요 영양소 만족 |
| 38 | + if (psum >= mp && fsum >= mf && ssum >= ms && vsum >= mv) { |
| 39 | + if (minRes > costsum) { |
| 40 | + minRes = costsum; |
| 41 | + resIdx = idx; |
| 42 | + } |
| 43 | + // 비용이 동일한 경우 번호가 작은 순서 |
| 44 | + else if (minRes == costsum) { |
| 45 | + if (resIdx > idx) { |
| 46 | + resIdx = idx; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + // 조건을 만족하는 답 x |
| 54 | + if (minRes == 1e9) { |
| 55 | + cout << -1; |
| 56 | + } else { |
| 57 | + cout << minRes << "\n"; |
| 58 | + for(auto iter : resIdx) { |
| 59 | + cout << iter << " "; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +// 백트래킹 |
| 67 | +#include <bits/stdc++.h> |
| 68 | +using namespace std; |
| 69 | + |
| 70 | +int N; |
| 71 | +int mp, mf, ms, mv; |
| 72 | +int p[16], f[16], s[16], v[16], c[16]; |
| 73 | +int res = 1e9; |
| 74 | + |
| 75 | +vector <int> temp; |
| 76 | +vector <int> vres; |
| 77 | + |
| 78 | +void dfs(int idx, int sp, int sf, int ss, int sv, int sc){ |
| 79 | + if (sp >= mp && sf >= mf && ss >= ms && sv >= mv) { |
| 80 | + if (res > sc) { |
| 81 | + res = sc; |
| 82 | + vres.clear(); |
| 83 | + for(auto iter: temp) { |
| 84 | + vres.push_back(iter); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + for(int i = idx + 1; i < N; i++){ |
| 90 | + temp.push_back(i + 1); |
| 91 | + dfs(i, sp + p[i], sf + f[i], ss + s[i], sv + v[i], sc + c[i]); |
| 92 | + temp.pop_back(); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +int main() { |
| 97 | + cin >> N; |
| 98 | + cin >> mp >> mf >> ms >> mv; // 최소 영양성분 |
| 99 | + |
| 100 | + for(int i = 0; i < N; i++){ |
| 101 | + cin >> p[i] >> f[i] >> s[i] >> v[i] >> c[i]; |
| 102 | + } |
| 103 | + dfs(-1, 0, 0, 0, 0, 0); |
| 104 | + if (res == 1e9) cout << -1 << "\n"; |
| 105 | + else cout << res << "\n"; |
| 106 | + |
| 107 | + for(auto iter: vres) { |
| 108 | + cout << iter << " "; |
| 109 | + } |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
0 commit comments