sacgajcvs

Untitled

Dec 16th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. /*
  2. _____ _ _ _ _
  3. |_ _| |__ ___ / \ _ __ ___| |__ _ _| |
  4. | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | |
  5. | | | | | | __// ___ \| | | \__ \ | | | |_| | |
  6. |_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_|
  7.  
  8. */
  9. #include<bits/stdc++.h>
  10. #include <ext/pb_ds/assoc_container.hpp>
  11. #include <ext/pb_ds/tree_policy.hpp>
  12. #define ll long long
  13. #define pb push_back
  14. #define ppb pop_back
  15. #define endl '\n'
  16. #define mii map<ll,ll>
  17. #define msi map<string,ll>
  18. #define mis map<ll, string>
  19. #define rep(i,a,b) for(ll i=a;i<b;i++)
  20. #define repr(i,a,b) for(ll i=b-1;i>=a;i--)
  21. #define trav(a, x) for(auto& a : x)
  22. #define pii pair<ll,ll>
  23. #define vi vector<ll>
  24. #define vii vector<pair<ll, ll>>
  25. #define vs vector<string>
  26. #define all(a) (a).begin(),(a).end()
  27. #define F first
  28. #define S second
  29. #define sz(x) (ll)x.size()
  30. #define hell 1000000007
  31. #define lbnd lower_bound
  32. #define ubnd upper_bound
  33.  
  34. /* For Debugging */
  35. #define DEBUG cerr<<"/n>>>I'm Here<<</n"<<endl;
  36. #define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
  37. #define what_is(x) cerr << #x << " is " << x << endl;
  38.  
  39. std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
  40. #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
  41. #define TIME cerr << "\nTime elapsed: " << setprecision(5) <<1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
  42. #define DECIMAL(n) cout << fixed ; cout << setprecision(n);
  43. #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  44. using namespace __gnu_pbds;
  45. using namespace std;
  46. #define PI 3.141592653589793
  47. #define N 20
  48. ll n,k,rem;
  49. ll a[N][N],dp[N][(1<<N)],cost[N][(1<<N)];
  50. void solve()
  51. {
  52. cin>>n>>k;
  53. ll sum=0;
  54. rep(i,0,n)
  55. {
  56. rep(j,0,n)
  57. {
  58. cin>>a[i][j];
  59. sum+=a[i][j];
  60. }
  61. }
  62. sum/=2;
  63. ll rem=n-k;
  64. rep(i,0,k)
  65. {
  66. rep(mask,0,(1<<rem))
  67. {
  68. cost[i][mask]=0;
  69. rep(x,0,rem)
  70. {
  71. if(mask&(1<<x))
  72. cost[i][mask]+=a[i][x+k];
  73. }
  74. rep(x,0,rem)
  75. {
  76. if(mask&(1<<x))
  77. {
  78. rep(y,x+1,rem)
  79. {
  80. if(mask&(1<<y))
  81. {
  82. cost[i][mask]+=a[x+k][y+k];
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. // rep(i,0,k)
  90. // {
  91. // rep(j,0,(1<<rem))
  92. // cout<<cost[i][j]<<" ";
  93. // cout<<endl;
  94. // }
  95. rep(mask,0,(1<<rem))
  96. {
  97. dp[0][mask]=cost[0][mask];
  98. }
  99. rep(i,1,k)
  100. {
  101. rep(mask,0,(1<<rem))
  102. {
  103. dp[i][mask]=0;
  104. for(ll j=mask;j>0;j=((j-1)&mask))
  105. {
  106. dp[i][mask]=max(dp[i][mask],dp[i-1][j]+cost[i][(j^mask)]);
  107. }
  108. dp[i][mask]=max(dp[i][mask],dp[i-1][0]+cost[i][mask]);
  109. }
  110. }
  111. // rep(i,0,k)
  112. // {
  113. // rep(j,0,(1<<rem))
  114. // cout<<dp[i][j]<<" ";
  115. // cout<<endl;
  116. // }
  117. cout<<sum-dp[k-1][(1<<rem)-1]<<endl;
  118. return;
  119. }
  120. int main()
  121. {
  122. FAST
  123. int TESTS=1;
  124. /************************************/
  125. // freopen("input.txt","r",stdin);
  126. // freopen("out.txt","w",stdout);
  127. /***********************************/
  128. cin>>TESTS;
  129. while(TESTS--)
  130. {
  131. solve();
  132. }
  133. TIME
  134. return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment