-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1102.cpp
More file actions
77 lines (58 loc) · 1.31 KB
/
1102.cpp
File metadata and controls
77 lines (58 loc) · 1.31 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include <string>
using namespace std;
int N, P;
int arr[16][16];
int dp[1 << 16];
const int INF = 123456789;
int minV = INF;
int repair(int visited, int count) {
if (count == P) return 0;
if (dp[visited] != -1)return dp[visited];
vector<int> notBreak;
vector<int> Break;
for (int i = 0; i < N; i++) {
if ((visited >> i) & 1)notBreak.push_back(i);
else Break.push_back(i);
}
int val = INF;
for (int i = 0; i < notBreak.size(); i++) {
for (int j = 0; j < Break.size(); j++) {
int nB = notBreak[i];
int B = Break[j];
val = min(val, repair(visited | (1 << B), count + 1) + arr[nB][B]);
}
}
return dp[visited] = val;
}
int main(void)
{
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
memset(dp, -1, sizeof(dp));
cin >> N;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
cin >> arr[i][j];
string temp; cin >> temp;
int visited = 0, count = 0;
for (int i = 0; i < N; i++) {
if (temp[i] == 'Y') {
visited = visited | (1 << i);
count++;
}
}
cin >> P;
if (count >= P) {
cout << "0";
return 0;
}
else if (count == 0) {
cout << "-1";
return 0;
}
cout << repair(visited, count);
}