-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1102.cpp
More file actions
44 lines (44 loc) · 1.01 KB
/
1102.cpp
File metadata and controls
44 lines (44 loc) · 1.01 KB
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
#include <bits/stdc++.h>
using namespace std;
int n, p, cost[16][16];
const int MAX = 123456789;
int memo[1 << 16];
int dp(int curState) {
int cnt = 0;
for(int i = 0; i < n; i++) {
if(curState & (1 << i)) cnt++;
}
if(cnt >= p) return 0;
int &ret = memo[curState];
if(ret != -1) return ret;
ret = 123456789;
for(int i = 0; i < n; i++) {
if(~curState & (1 << i)) {
for(int j = 0; j < n; j++) {
if(curState & (1 << j)) {
ret = min(ret, dp(curState | (1 << i)) + cost[j][i]);
}
}
}
}
return ret;
}
int main() {
memset(memo, -1, sizeof(memo));
cin >> n;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> cost[i][j];
}
}
int now = 0;
for(int i = 0; i < n; i++) {
char x; cin >> x;
if(x == 'Y') {
now |= (1 << i);
}
}
cin >> p;
if(dp(now) == MAX) cout << -1;
else cout << dp(now);
}