-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNastya and Unexpected Guest.cpp
129 lines (110 loc) · 2.89 KB
/
Nastya and Unexpected Guest.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <functional>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <sstream>
#include <unordered_set>
#include <unordered_map>
#include <sstream>
#include <set>
#include <cassert>
using namespace std;
#ifndef ONLINE_JUDGE
ifstream in("/Users/jiazhi.zhou/CLionProjects/acm/InOutput/input.txt");
#define Debug(v) cerr<<#v<<"="<<v<<'\n'
#endif
#ifdef ONLINE_JUDGE
istream& in = cin;
#define Debug(v) ;
#endif
typedef long long LL;
#define pii pair<int,int>
#define pll pair<LL,LL>
#define mp make_pair
#define pb push_back
#define lson (root<<1)
#define rson (root<<1|1)
#define FFLUAHALL fflush(stdin);fflush(stdout);
int n, m;
int d[10010];
int G, R;
int dp[10010][1010];
void input() {
in >> n >> m;
for (int i = 1; i <= m; ++i)
in >> d[i];
in >> G >> R;
}
void _init() {
for (int i = 0; i < 10010; i++)
for (int j = 0; j < 1010; ++j)
dp[i][j] = 1e9;
}
int bfs() {
deque<pii > Q;
Q.push_back(mp(1, G));
dp[1][G] = 0;
while (!Q.empty()) {
auto u = Q.front();
Q.pop_front();
int now = u.first;
int l = u.first - 1;
int r = u.first + 1;
if (l >= 1 && d[now] - d[l] <= u.second) {
int rest = u.second - (d[now] - d[l]);
if (rest > 0) {
if (dp[l][rest] > dp[u.first][u.second] + d[now] - d[l]) {
dp[l][rest] = dp[u.first][u.second] + d[now] - d[l];
Q.push_front(mp(l, rest));
}
} else {
if (dp[l][G] > dp[u.first][u.second] + d[now] - d[l] + R) {
dp[l][G] = dp[u.first][u.second] + d[now] - d[l] + R;
Q.push_back(mp(l, G));
}
}
}
if (r <= m && d[r] - d[now] <= u.second) {
int rest = u.second - (d[r] - d[now]);
if (rest > 0) {
if (dp[r][rest] > dp[u.first][u.second] + d[r] - d[now]) {
dp[r][rest] = dp[u.first][u.second] + d[r] - d[now];
Q.push_front(mp(r, rest));
}
} else {
if (dp[r][G] > dp[u.first][u.second] + d[r] - d[now] + R) {
dp[r][G] = dp[u.first][u.second] + d[r] - d[now] + R;
Q.push_back(mp(r, G));
}
}
}
}
int ans = 1e9;
for (int i = 0; i < 1010; ++i)
ans = min(ans, dp[m][i]);
if (dp[m][G] != 1e9)
ans = min(ans, dp[m][G] - R);
if (ans == 1e9)
ans = -1;
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int TEST_CASE = 1;
//in >> TEST_CASE;
while (TEST_CASE-- > 0) {
_init();
input();
sort(d + 1, d + m + 1);
cout << bfs() << endl;
}
return 0;
}