-
Notifications
You must be signed in to change notification settings - Fork 0
/
loj1011.cpp
67 lines (49 loc) · 1.03 KB
/
loj1011.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using namespace std;
#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#define maX(a,b) ((a>b)?(a):(b))
int Set(int N,int pos){return N=N | (1<<pos);}
//int reset(int N,int pos){return N= N & ~(1<<pos);}
bool check(int N,int pos){return (bool)(N & (1<<pos));}
int dp[1<<20];
int arr[20][20],n;
bool visited[1<<20];
int solve(int mask)
{
int k = __builtin_popcount(mask);
if(k==n)
{
return 0;
}
if(visited[mask])
return dp[mask];
int ans=0,i;
for(i=0;i<n;i++)
{
if(check(mask,i)==0)
{
ans=maX(ans,(solve(Set(mask,i))+arr[k][i]));
}
}
visited[mask]=true;
return dp[mask]=ans;
}
int main()
{
int T,Case=1;
scanf("%d",&T);
while(T--)
{
memset(visited,false,sizeof(visited));
scanf("%d",&n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
scanf("%d",&arr[i][j]);
}
printf("Case %d: %d\n",Case++,solve(0));
}
return 0;
}