Alex_tz307

Optimal Selection CPH

Sep 17th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3. // Optimal Selection - page 112 - CPH
  4. // k products, n days, each product ONCE and at most one per day. Minimum total price?
  5.  
  6. using namespace std;
  7.  
  8. int price[10][10], total[1 << 10][10];
  9.  
  10. int main() {
  11.     int k, n;
  12.     cin >> k >> n;
  13.     for(int i = 0; i < k; ++i)
  14.         for(int j = 0; j < n; ++j)
  15.             cin >> price[i][j];
  16.  
  17.     for(int i = 0; i < (1 << k); ++i)
  18.         for(int j = 0; j < n; ++j)
  19.             total[i][j] = INF;
  20.  
  21.     for(int x = 0; x < k; ++x)
  22.         total[1 << x][0] = price[x][0];
  23.  
  24.     for(int i = 0; i < n; ++i)
  25.         total[0][i] = 0;
  26.  
  27.     for(int d = 1; d < n; ++d)
  28.         for(int s = 0; s < (1 << k); ++s) {
  29.             total[s][d] = total[s][d - 1];
  30.             for(int x = 0; x < k; ++x)
  31.                 if(s & (1 << x))
  32.                     total[s][d] = min(total[s][d], total[s ^ (1 << x)][d - 1] + price[x][d]);
  33.         }
  34.  
  35.     cout << total[(1 << k) - 1][n - 1];
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment