Advertisement
KShah

Untitled

Mar 19th, 2022
893
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using std::cin;
  5. using std::cout;
  6. using std::vector;
  7. using std::pair;
  8.  
  9. const long long INF = 1e18;
  10.  
  11. bool isbit(long long mask, long long bit) {
  12.   return (mask >> bit) & 1;
  13. }
  14.  
  15. int main() {
  16.   int n, m;
  17.   cin >> n >> m;
  18.  
  19.   vector <long long> dist(n);
  20.   vector<vector<long long> > cost(n, vector<long long> (m));
  21.   for (int i = 0; i < n; ++i) {
  22.     cin >> dist[i];
  23.     for (int j = 0; j < m; ++j) {
  24.       cin >> cost[i][j];
  25.     }
  26.   }
  27.  
  28.   vector<vector<long long> > dp(n, vector <long long> (1 << m, INF));
  29.   for (int i = 0; i < n; ++i) {
  30.     for (int mask = 1; mask < (1 << m); ++mask) {
  31.       dp[i][mask] = dist[i];
  32.       for (int k = 0; k < m; ++k) {
  33.         if (isbit(mask, k)) {
  34.           dp[i][mask] += cost[i][k];
  35.         }
  36.       }
  37.     }
  38.   }
  39.  
  40.   vector<long long> mini(1 << m, INF);
  41.   for (int mask = 1; mask < (1 << m); ++mask) {
  42.     for (int submask = mask; submask; submask = (submask - 1) & mask) {
  43.       mini[mask] = std::min(mini[submask], mini[mask ^ submask]);
  44.     }
  45.   }
  46.  
  47.   std::cout << mini[(1 << m) - 1];
  48.  
  49.   return 0;
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement