Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define INF 0x3f3f3f3f
- // Optimal Selection - page 112 - CPH
- // k products, n days, each product ONCE and at most one per day. Minimum total price?
- using namespace std;
- int price[10][10], total[1 << 10][10];
- int main() {
- int k, n;
- cin >> k >> n;
- for(int i = 0; i < k; ++i)
- for(int j = 0; j < n; ++j)
- cin >> price[i][j];
- for(int i = 0; i < (1 << k); ++i)
- for(int j = 0; j < n; ++j)
- total[i][j] = INF;
- for(int x = 0; x < k; ++x)
- total[1 << x][0] = price[x][0];
- for(int i = 0; i < n; ++i)
- total[0][i] = 0;
- for(int d = 1; d < n; ++d)
- for(int s = 0; s < (1 << k); ++s) {
- total[s][d] = total[s][d - 1];
- for(int x = 0; x < k; ++x)
- if(s & (1 << x))
- total[s][d] = min(total[s][d], total[s ^ (1 << x)][d - 1] + price[x][d]);
- }
- cout << total[(1 << k) - 1][n - 1];
- }
Advertisement
Add Comment
Please, Sign In to add comment