-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboj.1102.cc
91 lines (77 loc) · 2.04 KB
/
boj.1102.cc
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef const ll cll;
typedef queue<ll> qll;
typedef queue<pll> qpll;
typedef priority_queue<ll> pqll;
typedef priority_queue<pll> pqpll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<vpll> vvpll;
#define FOR1(a, A) for (ll a = 0; a < A; ++a)
#define FOR2(a, b, A, B) \
for (ll a = 0; a < A; ++a) \
for (ll b = 0; b < B; ++b)
cll N = 16, INF = N * 36 + 1;
ll n, p, genStatus = 0, costs[N][N] = {{}}, dp[1 << N] = {};
// dp[0] => minCost, dp[1] => num of needed stations for minCost
ll num(ll status) {
ll result = 0;
for (ll i = 0; i < n; ++i) {
result += bool(status & (1 << i));
}
return result;
}
ll find(ll status) {
if (dp[status] <= INF) {
return dp[status];
}
ll result = INF;
for (ll prvStatus, prvCost, station = 0; station < n; ++station) {
if (!(status & (1 << station))) {
continue;
} else if (genStatus & (1 << station)) {
continue;
}
prvStatus = status & ~(1 << station);
prvCost = find(prvStatus);
for (ll prvStation = 0; prvStation < n; ++prvStation) {
if (!(prvStatus & (1 << prvStation))) {
continue;
}
result = min(result, prvCost + costs[prvStation][station]);
}
}
return dp[status] = result;
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
FOR2(i, l, n, n) { cin >> costs[i][l]; }
FOR1(i, n) {
char c;
cin >> c;
if (c == 'Y') {
genStatus |= (1 << i);
}
}
cin >> p;
memset(dp, 0x3f3f3f, sizeof(dp));
dp[genStatus] = 0;
ll result = INF;
for (ll status = 0; status < (1 << n); ++status) {
if (num(status | genStatus) < p) {
continue;
}
result = min(result, find(status | genStatus));
}
cout << (result == INF ? -1 : result) << "\n";
return 0;
}