#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> II;
const int N = 5e2 + 5, M = 1e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
int h, w, a, b, c, dirs[5] = {0, 1, 0, -1, 0}, n, s[M], t[M];
ll dist[N][N][5], near[N][N];
inline bool inside(int x, int y){
return x >= 0 && x <= h && y >= 0 && y <= w;
}
void multiBFS(){
for(int i = 0; i <= h; i ++){
for(int j = 0; j <= w; j ++) near[i][j] = -1;
}
queue<II> qu;
for(int i = 1; i <= n; i ++) near[s[i]][t[i]] = 0, qu.push(II(s[i], t[i]));
while(!qu.empty()){
int x = qu.front().first, y = qu.front().second;
qu.pop();
for(int i = 0; i < 4; i ++){
int nx = x + dirs[i], ny = y + dirs[i + 1];
if(inside(nx, ny) && near[nx][ny] == -1){
near[nx][ny] = near[x][y] + c;
qu.push(II(nx, ny));
}
}
}
}
struct pack{
ll du;
int x, y, lab;
pack(ll _du = 0, int _x = 0, int _y = 0, int _lab = 0) : du(_du), x(_x), y(_y), lab(_lab) {}
bool operator > (const pack& op) const{
return du > op.du;
}
};
void Dijkstra(){
for(int i = 0; i <= h; i ++) for(int j = 0; j <= w; j ++) for(int k = 0; k < 5; k ++) dist[i][j][k] = INF;
dist[s[1]][t[1]][4] = 0;
priority_queue<pack, vector<pack>, greater<pack>> pq;
pq.push(pack(0, s[1], t[1], 4));
while(!pq.empty()){
ll du = pq.top().du;
int x = pq.top().x, y = pq.top().y, lab = pq.top().lab;
pq.pop();
if(du != dist[x][y][lab]) continue;
if(lab != 4){
int nx = x + dirs[lab], ny = y + dirs[lab + 1];
if(inside(nx, ny) && dist[nx][ny][lab] > du + a){
dist[nx][ny][lab] = du + a;
pq.push(pack(du + a, nx, ny, lab));
}
if(dist[x][y][4] > du + near[x][y]){
dist[x][y][4] = du + near[x][y];
pq.push(pack(dist[x][y][4], x, y, 4));
}
}
else{
for(int i = 0; i < 4; i ++){
int nx = x + dirs[i], ny = y + dirs[i + 1];
if(!inside(nx, ny)) continue;
if(dist[nx][ny][4] > du + c){
dist[nx][ny][4] = du + c;
pq.push(pack(du + c, nx, ny, 4));
}
if(dist[nx][ny][i] > du + a + b){
dist[nx][ny][i] = du + a + b;
pq.push(pack(du + a + b, nx, ny, i));
}
}
}
}
cout << dist[s[n]][t[n]][4];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> h >> w >> a >> b >> c >> n;
for(int i = 1; i <= n; i ++) cin >> s[i] >> t[i];
multiBFS();
Dijkstra();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
6736 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
273 ms |
18448 KB |
Output is correct |
4 |
Correct |
307 ms |
18496 KB |
Output is correct |
5 |
Correct |
43 ms |
6912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
18544 KB |
Output is correct |
2 |
Correct |
314 ms |
18468 KB |
Output is correct |
3 |
Correct |
226 ms |
16132 KB |
Output is correct |
4 |
Correct |
223 ms |
18492 KB |
Output is correct |
5 |
Correct |
231 ms |
18460 KB |
Output is correct |
6 |
Correct |
252 ms |
18528 KB |
Output is correct |
7 |
Correct |
322 ms |
18572 KB |
Output is correct |
8 |
Correct |
294 ms |
18540 KB |
Output is correct |
9 |
Correct |
315 ms |
18684 KB |
Output is correct |
10 |
Correct |
39 ms |
7540 KB |
Output is correct |
11 |
Correct |
304 ms |
18660 KB |
Output is correct |
12 |
Correct |
318 ms |
18560 KB |
Output is correct |
13 |
Correct |
185 ms |
16072 KB |
Output is correct |
14 |
Correct |
317 ms |
18632 KB |
Output is correct |
15 |
Correct |
237 ms |
18456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
6736 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
273 ms |
18448 KB |
Output is correct |
4 |
Correct |
307 ms |
18496 KB |
Output is correct |
5 |
Correct |
43 ms |
6912 KB |
Output is correct |
6 |
Correct |
317 ms |
18544 KB |
Output is correct |
7 |
Correct |
314 ms |
18468 KB |
Output is correct |
8 |
Correct |
226 ms |
16132 KB |
Output is correct |
9 |
Correct |
223 ms |
18492 KB |
Output is correct |
10 |
Correct |
231 ms |
18460 KB |
Output is correct |
11 |
Correct |
252 ms |
18528 KB |
Output is correct |
12 |
Correct |
322 ms |
18572 KB |
Output is correct |
13 |
Correct |
294 ms |
18540 KB |
Output is correct |
14 |
Correct |
315 ms |
18684 KB |
Output is correct |
15 |
Correct |
39 ms |
7540 KB |
Output is correct |
16 |
Correct |
304 ms |
18660 KB |
Output is correct |
17 |
Correct |
318 ms |
18560 KB |
Output is correct |
18 |
Correct |
185 ms |
16072 KB |
Output is correct |
19 |
Correct |
317 ms |
18632 KB |
Output is correct |
20 |
Correct |
237 ms |
18456 KB |
Output is correct |
21 |
Correct |
55 ms |
7128 KB |
Output is correct |
22 |
Correct |
368 ms |
18524 KB |
Output is correct |
23 |
Correct |
348 ms |
14388 KB |
Output is correct |
24 |
Correct |
369 ms |
15612 KB |
Output is correct |
25 |
Correct |
334 ms |
18504 KB |
Output is correct |
26 |
Correct |
331 ms |
18732 KB |
Output is correct |
27 |
Correct |
134 ms |
14072 KB |
Output is correct |
28 |
Correct |
163 ms |
14848 KB |
Output is correct |
29 |
Correct |
312 ms |
17312 KB |
Output is correct |
30 |
Correct |
147 ms |
14044 KB |
Output is correct |
31 |
Correct |
329 ms |
18532 KB |
Output is correct |
32 |
Correct |
412 ms |
20708 KB |
Output is correct |
33 |
Correct |
275 ms |
18492 KB |
Output is correct |
34 |
Correct |
386 ms |
18620 KB |
Output is correct |
35 |
Correct |
141 ms |
14668 KB |
Output is correct |