-
Notifications
You must be signed in to change notification settings - Fork 0
/
directi21.cpp
69 lines (52 loc) · 977 Bytes
/
directi21.cpp
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;
int p;
int k;
int getMinPath (int arr[][2], int d[][2], int i, int j, int n ) {
if (d[i][j] != -1) {
return d[i][j];
}
if (i == n -1) {
return arr[i][j];
}
int min = INT_MAX;
for (int x = i+1; x <= i + k && x < n; x++) {
int cur = arr[i][j] + getMinPath(arr, d, x, j, n);
if (min > cur) {
min = cur;
}
}
for (int x = i+1; x <= i + k && x < n; x++) {
int cur = arr[i][j] + getMinPath(arr, d, x, (j+1)%2, n) + p;
if (min > cur) {
min = cur;
}
}
d[i][j] = min;
return d[i][j];
}
int main()
{
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
cin >> k;
cin >> p;
int arr[n][2];
for (int i = 0; i < n; ++i) {
cin >> arr[i][0];
}
for (int i = 0; i < n; ++i) {
cin >> arr[i][1];
}
int d[n][2];
for (int i = 0; i < n; i++ ) {
d[i][0] = -1;
d[i][1] = -1;
}
cout << min(getMinPath(arr, d, 0, 0, n), getMinPath(arr, d, 0, 1, n)) << endl;
}
return 0;
}