-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1102.cpp
More file actions
80 lines (77 loc) · 1.42 KB
/
1102.cpp
File metadata and controls
80 lines (77 loc) · 1.42 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
78
79
80
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#define INF 987654321
using namespace std;
int N, P, answer = INF;
vector<int> dp;
vector<vector<int>> cost;
int getCost(int bit, int target)
{
int re = INF;
for (int i = 0; i < N; i++)
{
if (bit & (1 << i))
{
re = min(re, cost[i][target]);
}
}
return re;
}
void dfs(int bit, int cnt, int acc)
{
if (cnt >= P)
{
answer = min(answer, acc);
}
for (int i = 0; i < N; i++)
{
if (!(bit & (1 << i)))
{
int nextBit = bit | (1 << i);
int nextCost = acc + getCost(bit, i);
if (dp[nextBit] > nextCost)
{
dp[nextBit] = nextCost;
dfs(nextBit, cnt + 1, nextCost);
}
}
}
}
int main()
{
int cnt = 0, bitmask = 0;
string s;
cin >> N;
cost.assign(N, vector<int>(N, 0));
dp.assign(1 << N, INF);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cin >> cost[i][j];
}
}
cin >> s;
for (int i = 0; i < N; i++)
{
if (s[i] == 'Y')
{
bitmask |= (1 << i);
cnt++;
}
}
cin >> P;
dp[bitmask] = 0;
dfs(bitmask, cnt, 0);
if (answer == INF)
{
cout << -1 << endl;
}
else
{
cout << answer << endl;
}
return 0;
}