#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
using tiii = tuple<ll,int,int,int>;
const ll inf = 1e13 + 5;
const int nmax = 505;
int H, w;
int A, B, C;
ll mat[nmax][nmax][5];
ll dist[nmax][nmax];
int dirx[4] = {1, -1, 0, 0};
int diry[4] = {0, 0, 1, -1};
signed main() {
cin >> H >> w;
cin >> A >> B >> C;
++H;
++w;
for(int i = 1; i <= H; i++) {
for(int j = 1; j <= w; j++) {
dist[i][j] = inf;
for(int h = 0; h < 5; h++)
mat[i][j][h] = inf;
}
}
int q;
cin >> q;
int ss = 1, ts = 1, sf = 1, tf = 1;
queue<pii> que;
for(int i = 0, x, y; i < q; i++) {
cin >> x >> y;
++x;
++y;
dist[x][y] = 0;
que.emplace(x, y);
if(i == 0)
tie(ss, ts) = pii{x, y};
tie(sf, tf) = pii{x, y};
}
while(!que.empty()) {
auto [x, y] = que.front();
que.pop();
for(int h = 0; h < 4; h++) {
if(dist[x + dirx[h]][y + diry[h]] > dist[x][y] + C) {
que.emplace(x + dirx[h], y + diry[h]);
dist[x + dirx[h]][y + diry[h]] = dist[x][y] + C;
}
}
}
priority_queue<tiii, vector<tiii>, greater<tiii>> heap;
heap.emplace(0, ss, ts, 4);
mat[ss][ts][4] = 0;
while(!heap.empty()) {
auto [cost, x, y, mode] = heap.top();
heap.pop();
//if(mode != 4)
//cerr << "> " << mat[x][y][mode] << "==" << cost << ' ' << x << ' ' << y << ' ' << mode << '\n';
if(cost > mat[x][y][mode]) continue;
//if(mode != 4)
//cerr << cost << ' ' << x << ' ' << y << ' ' << mode << '\n';
if(mode == 4) {
for(int h = 0; h < 4; h++) {
if(mat[x + dirx[h]][y + diry[h]][mode] > mat[x][y][mode] + C) {
mat[x + dirx[h]][y + diry[h]][mode] = mat[x][y][mode] + C;
//cerr << "+ " << x + dirx[h] << ' ' << y + diry[h] << ' ' << mode << '\n';
heap.emplace(mat[x][y][mode] + C, x + dirx[h], y + diry[h], mode);
}
if(mat[x][y][h] > mat[x][y][mode] + B) {
//cerr << "+ " << x << ' ' << y << ' ' << h << '\n';
mat[x][y][h] = mat[x][y][mode] + B;
heap.emplace(mat[x][y][h], x, y, h);
}
}
}
else {
if(mat[x + dirx[mode]][y + diry[mode]][mode] > mat[x][y][mode] + A) {
mat[x + dirx[mode]][y + diry[mode]][mode] = mat[x][y][mode] + A;
//cerr << "+ " << x + dirx[mode] << ' ' << y + diry[mode] << ' ' << mode << '\n';
heap.emplace(mat[x][y][mode] + A, x + dirx[mode], y + diry[mode], mode);
}
if(mat[x][y][4] > mat[x][y][mode] + dist[x][y]) {
mat[x][y][4] = mat[x][y][mode] + dist[x][y];
heap.emplace(mat[x][y][4], x, y, 4);
}
}
}
ll mn = inf;
for(int h = 0; h < 5; h++)
mn = min(mat[sf][tf][h], mn);
cout << mn << '\n';
}
/**
Vom spune că toamna a venit... foarte trist -
-- George Bacovia, Scântei galbene
*/
Compilation message
soccer.cpp: In function 'int main()':
soccer.cpp:58:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | auto [x, y] = que.front();
| ^
soccer.cpp:72:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
72 | auto [cost, x, y, mode] = heap.top();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
6736 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
283 ms |
18488 KB |
Output is correct |
4 |
Correct |
290 ms |
18488 KB |
Output is correct |
5 |
Correct |
76 ms |
6788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
340 ms |
18488 KB |
Output is correct |
2 |
Correct |
354 ms |
18480 KB |
Output is correct |
3 |
Correct |
245 ms |
16056 KB |
Output is correct |
4 |
Correct |
234 ms |
18388 KB |
Output is correct |
5 |
Correct |
252 ms |
18412 KB |
Output is correct |
6 |
Correct |
251 ms |
18384 KB |
Output is correct |
7 |
Correct |
301 ms |
18548 KB |
Output is correct |
8 |
Correct |
321 ms |
18548 KB |
Output is correct |
9 |
Correct |
345 ms |
18616 KB |
Output is correct |
10 |
Correct |
59 ms |
7620 KB |
Output is correct |
11 |
Correct |
339 ms |
18560 KB |
Output is correct |
12 |
Correct |
327 ms |
18524 KB |
Output is correct |
13 |
Correct |
191 ms |
16060 KB |
Output is correct |
14 |
Correct |
323 ms |
18604 KB |
Output is correct |
15 |
Correct |
290 ms |
18512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
6736 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
283 ms |
18488 KB |
Output is correct |
4 |
Correct |
290 ms |
18488 KB |
Output is correct |
5 |
Correct |
76 ms |
6788 KB |
Output is correct |
6 |
Correct |
340 ms |
18488 KB |
Output is correct |
7 |
Correct |
354 ms |
18480 KB |
Output is correct |
8 |
Correct |
245 ms |
16056 KB |
Output is correct |
9 |
Correct |
234 ms |
18388 KB |
Output is correct |
10 |
Correct |
252 ms |
18412 KB |
Output is correct |
11 |
Correct |
251 ms |
18384 KB |
Output is correct |
12 |
Correct |
301 ms |
18548 KB |
Output is correct |
13 |
Correct |
321 ms |
18548 KB |
Output is correct |
14 |
Correct |
345 ms |
18616 KB |
Output is correct |
15 |
Correct |
59 ms |
7620 KB |
Output is correct |
16 |
Correct |
339 ms |
18560 KB |
Output is correct |
17 |
Correct |
327 ms |
18524 KB |
Output is correct |
18 |
Correct |
191 ms |
16060 KB |
Output is correct |
19 |
Correct |
323 ms |
18604 KB |
Output is correct |
20 |
Correct |
290 ms |
18512 KB |
Output is correct |
21 |
Correct |
72 ms |
6812 KB |
Output is correct |
22 |
Correct |
374 ms |
18524 KB |
Output is correct |
23 |
Correct |
341 ms |
14200 KB |
Output is correct |
24 |
Correct |
374 ms |
15552 KB |
Output is correct |
25 |
Correct |
309 ms |
18412 KB |
Output is correct |
26 |
Correct |
359 ms |
18640 KB |
Output is correct |
27 |
Correct |
262 ms |
13644 KB |
Output is correct |
28 |
Correct |
253 ms |
14096 KB |
Output is correct |
29 |
Correct |
386 ms |
16768 KB |
Output is correct |
30 |
Correct |
212 ms |
13812 KB |
Output is correct |
31 |
Correct |
343 ms |
18564 KB |
Output is correct |
32 |
Correct |
411 ms |
19888 KB |
Output is correct |
33 |
Correct |
262 ms |
18484 KB |
Output is correct |
34 |
Correct |
377 ms |
18568 KB |
Output is correct |
35 |
Correct |
218 ms |
13800 KB |
Output is correct |