forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathF.cpp
More file actions
69 lines (63 loc) · 1.4 KB
/
F.cpp
File metadata and controls
69 lines (63 loc) · 1.4 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
#include <bits/stdc++.h>
using namespace std;
const int oo = 1 << 30;
int n, k, W, a[444][444], c[444][444], f[444][444];
void dp(int mask)
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
f[i][j] = oo;
if (mask >> c[i][j] & 1)
f[i][j] = min(f[i - 1][j], f[i][j - 1]) + a[i][j];
}
}
int main()
{
scanf("%d%d%d", &n, &k, &W);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
scanf("%d", a[i] + j);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
scanf("%d", c[i] + j);
c[i][j]--;
}
for (int i = 2; i <= n; i++)
f[i][0] = f[0][i] = oo;
int ans = oo, bestMask = -1;
for (int mask = 1; mask < 1 << k; mask++)
{
if (!(mask >> c[1][1] & 1) || !(mask >> c[n][n] & 1))
continue;
int bit = __builtin_popcount(mask);
if (bit < ans)
{
dp(mask);
if (f[n][n] <= W)
{
ans = bit;
bestMask = mask;
}
}
}
if (ans == oo) cout << -1 << endl;
else
{
cout << ans << endl;
dp(bestMask);
vector <int> path;
for (int i = n, j = n; i && j;)
{
path.push_back(j);
path.push_back(i);
if (f[i][j] == f[i - 1][j] + a[i][j]) i--;
else j--;
}
reverse(path.begin(), path.end());
for (auto x : path)
printf("%d ", x);
puts("");
}
}