Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const ll mod = 1000000007;
- #define si(x) scanf("%d",&x)
- #define sil(x) scanf("%lld",&x)
- #define pi(x) printf("%d\n",x)
- #define pil(x) printf("%lld\n",x)
- #define rep(i,n,m) for(int i = n; i< m; i++)
- #define reps(i,n,m) for(int i = n; i >= m; i--)
- int dp[17][(1<<17)],a[17][17],t,n,cas;
- int Set(int N,int pos)
- {
- //this fix a woman for a man
- return N = N | (1 << pos);
- }
- int Reset(int N,int pos)
- {
- return N = N & ~(1 << pos);
- }
- bool check(int N,int pos)
- {
- //this checks if this pos is empty or is not possessed by any man
- return (bool)(N & (1<<pos));
- }
- //bitmask dp
- //bitmask dp is needed when all possible set is to be generated
- int go(int men,int women)
- {
- //base case is when all men are married
- if(men >= n) return 0;
- if(dp[men][women] != -1) return dp[men][women];
- int ret = 0;
- for(int i = 0; i < n; i++){
- if(!check(women,i)){
- //if women "i" is single for this time for man "men" then we can arrange
- //a marriage and check whether it is in a set of of the maximum answer
- ret = max(ret,a[men][i] + go(men+1,Set(women,i)));
- }
- }
- return dp[men][women] = ret;
- }
- //it will always make monogamous family because we choose men one by one.
- int main()
- {
- cin >> t;
- while(t--){
- cin >> n
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n; j++){
- cin >> a[i][j];
- }
- }
- memset(dp,-1,sizeof(dp));
- int ans = go(0,0);
- printf("Case %d: %d\n",++cas,ans);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement