Advertisement
Abrar_Al_Samit

Pimp My Ride (LOJ 1119)

Jul 28th, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7.  
  8.  
  9. #define debug(x) cerr << '[' << (#x) << "] = " << x << '\n'
  10.  
  11. template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
  12.  
  13. int N, g[14][14], dp[14][1<<14];
  14. int solve(int i, int mask) {
  15.     int &ret = dp[i][mask];
  16.     if(ret!=-1) return ret;
  17.     int cost = 0;
  18.     ret = INT_MAX;
  19.     for(int j=0; j<N; ++j) {
  20.         if(~mask >> j & 1) {
  21.             ret = min(ret, solve(j, mask|(1<<j)));
  22.         }
  23.         else cost += g[i][j];
  24.     }
  25.     if(ret==INT_MAX) ret = 0;
  26.     ret += cost;
  27.     return ret;
  28. }
  29. void PlayGround(int T) {
  30.     cin >> N;
  31.     for(int i=0; i<N; ++i) {
  32.         for(int j=0; j<(1<<N); ++j) {
  33.             dp[i][j] = -1;
  34.         }
  35.     }
  36.     for(int i=0; i<N; ++i) {
  37.         for(int j=0; j<N; ++j) {
  38.             cin >> g[i][j];
  39.         }
  40.     }
  41.     int ans = INT_MAX;
  42.     for(int i=0; i<N; ++i)
  43.         ans = min(ans, solve(i, (1<<i)));
  44.     cout << "Case " << T << ": " << ans << '\n';
  45.  
  46.     #ifndef ONLINE_JUDGE
  47.         cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  48.     #endif
  49. }
  50. int main() {
  51.     ios_base::sync_with_stdio(0);
  52.     cin.tie(0);
  53.     cout.tie(0);
  54.     #ifndef ONLINE_JUDGE
  55.         freopen("input.txt", "r", stdin);
  56.     #endif
  57.     int T;
  58.     cin >> T;
  59.     for(int i=1; i<=T; ++i)
  60.     PlayGround(i);
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement