Advertisement
Underdogs

Untitled

Feb 13th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. const ll mod = 1000000007;
  6. #define si(x) scanf("%d",&x)
  7. #define sil(x) scanf("%lld",&x)
  8. #define pi(x) printf("%d\n",x)
  9. #define pil(x) printf("%lld\n",x)
  10. #define rep(i,n,m) for(int i = n; i< m; i++)
  11. #define reps(i,n,m) for(int i = n; i >= m; i--)
  12.  
  13. int dp[17][(1<<17)],a[17][17],t,n,cas;
  14.  
  15. int Set(int N,int pos)
  16. {
  17. //this fix a woman for a man
  18. return N = N | (1 << pos);
  19. }
  20.  
  21. int Reset(int N,int pos)
  22. {
  23. return N = N & ~(1 << pos);
  24. }
  25.  
  26. bool check(int N,int pos)
  27. {
  28. //this checks if this pos is empty or is not possessed by any man
  29. return (bool)(N & (1<<pos));
  30. }
  31. //bitmask dp
  32. //bitmask dp is needed when all possible set is to be generated
  33. int go(int men,int women)
  34. {
  35. //base case is when all men are married
  36. if(men >= n) return 0;
  37.  
  38. if(dp[men][women] != -1) return dp[men][women];
  39.  
  40. int ret = 0;
  41.  
  42. for(int i = 0; i < n; i++){
  43. if(!check(women,i)){
  44. //if women "i" is single for this time for man "men" then we can arrange
  45. //a marriage and check whether it is in a set of of the maximum answer
  46. ret = max(ret,a[men][i] + go(men+1,Set(women,i)));
  47. }
  48. }
  49.  
  50. return dp[men][women] = ret;
  51.  
  52. }
  53. //it will always make monogamous family because we choose men one by one.
  54. int main()
  55. {
  56. cin >> t;
  57.  
  58. while(t--){
  59. cin >> n
  60.  
  61. for(int i = 0; i < n; i++){
  62. for(int j = 0; j < n; j++){
  63. cin >> a[i][j];
  64. }
  65. }
  66.  
  67. memset(dp,-1,sizeof(dp));
  68.  
  69. int ans = go(0,0);
  70.  
  71. printf("Case %d: %d\n",++cas,ans);
  72. }
  73.  
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement