#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int n, m, k, x[100100], y[100100], X[505], Y[505];
ll A, B, C, D[505][505], d[505][505][2];
vector<int> vx[505], vy[505];
queue<array<int, 3>> q;
priority_queue<tuple<ll, int, int, int>> pq;
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m >> A >> B >> C >> k;
for(int i=1;i<=k;i++) cin >> x[i] >> y[i];
for(int i=0;i<=n;i++) for(int j=0;j<=m;j++) {
D[i][j] = d[i][j][0] = d[i][j][1] = INF;
}
for(int i=1;i<k;i++) D[x[i]][y[i]] = 0, q.push({0, x[i], y[i]});
while(!q.empty()) {
auto [c, x, y] = q.front(); q.pop();
for(int i=0;i<4;i++) {
int xx = x+dx[i], yy = y+dy[i];
if(xx < 0 || yy < 0 || xx > n || yy > m || D[xx][yy] <= c+1) continue;
D[xx][yy] = c+1, q.push({c+1, xx, yy});
}
}
for(int i=1;i<k;i++) {
vx[x[i]].push_back(y[i]);
vy[y[i]].push_back(x[i]);
}
memset(X, -1, sizeof X), memset(Y, -1, sizeof Y);
d[x[1]][y[1]][0] = 0, pq.push({0, x[1], y[1], 0});
while(!pq.empty()) {
auto [c, x, y, f] = pq.top(); pq.pop();
if(d[x][y][f] < -c) continue;
// cout << -c << ' ' << x << ' ' << y << " " << f << endl;
if(f) {
if(X[x] < 0) {
X[x] = y;
for(auto y : vx[x]) {
ll C = abs(y-X[x])*A+B-c;
if(C < d[x][y][0]) d[x][y][0] = C, pq.push({-C, x, y, 0});
}
}
if(Y[y] < 0) {
Y[y] = x;
for(auto x : vy[y]) {
ll C = abs(x-Y[y])*A+B-c;
if(C < d[x][y][0]) d[x][y][0] = C, pq.push({-C, x, y, 0});
}
}
}
for(int i=0;i<4;i++) {
int xx = x+dx[i], yy = y+dy[i];
if(X[xx] < 0 && Y[yy] < 0) continue;
ll C = min(X[xx] >= 0 ? d[xx][X[xx]][1] + abs(yy-X[xx])*A+B : INF, Y[yy] >= 0 ? d[Y[yy]][yy][1] + abs(xx-Y[yy])*A+B : INF);
if(xx < 0 || yy < 0 || xx > n || yy > m || d[xx][yy][0] <= C) continue;
d[xx][yy][0] = C, pq.push({-C, xx, yy, 0});
}
if(!f) {
if(d[x][y][1] > D[x][y]*C-c) d[x][y][1] = D[x][y]*C-c, pq.push({c-D[x][y]*C, x, y, 1});
continue;
}
if(d[x][y][0] > -c) d[x][y][0] = -c, pq.push({c, x, y, 0});
for(int i=0;i<4;i++) {
int xx = x+dx[i], yy = y+dy[i];
if(xx < 0 || yy < 0 || xx > n || yy > m || d[xx][yy][1] <= C-c) continue;
d[xx][yy][1] = C-c, pq.push({c-C, xx, yy, 1});
}
}
cout << min(d[x[k]][y[k]][0], d[x[k]][y[k]][1]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
5324 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
170 ms |
12520 KB |
Output is correct |
4 |
Correct |
152 ms |
12564 KB |
Output is correct |
5 |
Correct |
59 ms |
4568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
12596 KB |
Output is correct |
2 |
Correct |
248 ms |
12572 KB |
Output is correct |
3 |
Correct |
145 ms |
11332 KB |
Output is correct |
4 |
Correct |
113 ms |
12564 KB |
Output is correct |
5 |
Correct |
154 ms |
12636 KB |
Output is correct |
6 |
Correct |
130 ms |
12560 KB |
Output is correct |
7 |
Correct |
208 ms |
12776 KB |
Output is correct |
8 |
Correct |
197 ms |
12616 KB |
Output is correct |
9 |
Correct |
204 ms |
12764 KB |
Output is correct |
10 |
Correct |
33 ms |
6780 KB |
Output is correct |
11 |
Correct |
259 ms |
12752 KB |
Output is correct |
12 |
Correct |
224 ms |
12600 KB |
Output is correct |
13 |
Correct |
130 ms |
11456 KB |
Output is correct |
14 |
Correct |
212 ms |
12700 KB |
Output is correct |
15 |
Correct |
218 ms |
12720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
5324 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
170 ms |
12520 KB |
Output is correct |
4 |
Correct |
152 ms |
12564 KB |
Output is correct |
5 |
Correct |
59 ms |
4568 KB |
Output is correct |
6 |
Correct |
195 ms |
12596 KB |
Output is correct |
7 |
Correct |
248 ms |
12572 KB |
Output is correct |
8 |
Correct |
145 ms |
11332 KB |
Output is correct |
9 |
Correct |
113 ms |
12564 KB |
Output is correct |
10 |
Correct |
154 ms |
12636 KB |
Output is correct |
11 |
Correct |
130 ms |
12560 KB |
Output is correct |
12 |
Correct |
208 ms |
12776 KB |
Output is correct |
13 |
Correct |
197 ms |
12616 KB |
Output is correct |
14 |
Correct |
204 ms |
12764 KB |
Output is correct |
15 |
Correct |
33 ms |
6780 KB |
Output is correct |
16 |
Correct |
259 ms |
12752 KB |
Output is correct |
17 |
Correct |
224 ms |
12600 KB |
Output is correct |
18 |
Correct |
130 ms |
11456 KB |
Output is correct |
19 |
Correct |
212 ms |
12700 KB |
Output is correct |
20 |
Correct |
218 ms |
12720 KB |
Output is correct |
21 |
Correct |
56 ms |
5552 KB |
Output is correct |
22 |
Incorrect |
204 ms |
12624 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |