Advertisement
Maruf_Hasan

1011

Oct 18th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. //In the Name of Allah Most Gracious, Most Merciful//
  2. /*If you want something you've never had, you have to do something you never did.*/
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6.  
  7.  
  8. #define pb push_back
  9. #define ll long long
  10. #define pii pair<int,int>
  11. #define pll pair<ll,ll>
  12. #define M 100007
  13. #define INF 1e9
  14. #define INFL 1e18
  15. #define PI acos(-1)
  16. #define mp make_pair
  17.  
  18. //const int fx[]= {+1,-1,+0,+0};
  19. //const int fy[]= {+0,+0,+1,-1};
  20. //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
  21. //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
  22. //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
  23. //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
  24. int arr[18][18];
  25. int n;
  26. int dp[18][(1<<17)+10];
  27.  
  28. ll mydp(int pos,int mask)
  29. {
  30. if(pos>=n)
  31. {
  32. return 0;
  33. }
  34. if(dp[pos][mask]!=-1)
  35. return dp[pos][mask];
  36. int ret=0;
  37. for(int i=0;i<n;i++)
  38. {
  39. int nmask=(mask & (1<<i));
  40. if(nmask!=0)
  41. continue;
  42. int setbit=mask |(1<<i);
  43. int x=arr[pos][i]+mydp(pos+1,setbit);
  44. // cout<< (mask|(1<<i)) <<endl;
  45. // cout<<x<<endl;
  46. ret=max(ret,x);
  47. }
  48. // cout<<ret<<endl;
  49. dp[pos][mask]=ret;
  50. }
  51.  
  52.  
  53. int main()
  54. {
  55.  
  56. // #ifndef ONLINE_JUDGE
  57. // freopen("input.txt","r",stdin);
  58. // freopen("1011.txt","w",stdout);
  59. // #endif
  60. // cout<<(1<<16)<<endl;
  61. // return 0;
  62. int t,kase=0;
  63. cin>>t;
  64. while(t--)
  65. {
  66.  
  67. cin>>n;
  68. for(int i=0;i<n;i++)
  69. {
  70. for(int j=0;j<n;j++)
  71. {
  72. cin>>arr[i][j];
  73. }
  74. }
  75. memset(dp,-1,sizeof dp);
  76. int ans=mydp(0,0);
  77. printf("Case %d: %d\n",kase+1,ans);
  78. kase++;
  79. //memset(arr,0,sizeof arr);
  80. }
  81.  
  82.  
  83. ///Before submit=>
  84. /// *check for integer overflow,array bounds
  85. /// *check for n=1
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement