-
Notifications
You must be signed in to change notification settings - Fork 2
/
UVA 11218 - KTV.cpp
54 lines (47 loc) · 1.28 KB
/
UVA 11218 - KTV.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
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
int n, memo[(1<<9)], valid[10][10][10];
int dp(int u)
{
// If all the first 9 bits of u are set ( each person is in a group ),
// then this is a possible answer.
if(u == (1<<9) - 1) return 0;
// If this state was reached before, return the answer.
int &ret = memo[u];
if(ret != -1) return ret;
// Try all possible combinations left.
ret = -1000000;
int temp;
for(int i=0; i<9; i++){
if((1<<i) & u) continue;
for(int j = i + 1; j<9; j++){
if((1<<j) & u) continue;
for(int k = j+1; k<9; k++){
if((1<<k) & u || valid[i][j][k] == -1) continue;
temp = u | (1<<i) | (1<<j) | (1<<k);
ret = max(ret, dp(temp) + valid[i][j][k] );
}
}
}
return ret;
}
int main()
{
int tc = 1;
while(1){
scanf("%d", &n);
if(!n) return 0;
memset(valid, -1, sizeof valid);
memset(memo, -1, sizeof memo);
int a, b, c, s;
for(int i=0; i<n; i++){
scanf("%d%d%d%d", &a, &b, &c, &s);
a--; b--; c--;
valid[a][b][c] = s;
}
int ans = dp(0);
if(ans < 0) ans = -1;
printf("Case %d: %d\n", tc++, ans );
}
}