#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cassert>
#include <ctime>
#include <chrono>
#include <cstdio>
#include <random>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <deque>
#include <queue>
#include <bitset>
#include <list>
#include <fstream>
#include <functional>
#include <complex>
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
const long long INF = 1e18, MOD = 1e9 + 7, MOD2 = 998244353, LOG = 18;
const long double EPS = 1e-9, PI = acos(-1);
const long long dx[4] = { -1, 0, 1, 0 }, dy[4] = { 0, 1, 0, -1 };
void solve()
{
long long n, m, a, b, c, k;
vector<pair<long long, long long>> v;
cin >> n >> m >> a >> b >> c >> k;
v.resize(k);
n++; m++;
for (long long i = 0; i < k; i++)
{
cin >> v[i].first >> v[i].second;
}
vector<vector<long long>> near(n, vector<long long>(m, INF));
deque<pair<long long, long long>> bq;
for (long long i = 0; i < k; i++)
{
near[v[i].first][v[i].second] = 0;
bq.push_back({ v[i].first, v[i].second });
}
auto inside = [&](long long x, long long y) -> bool
{
return 0 <= x && x < n && 0 <= y && y < m;
};
while (bq.size())
{
auto [x, y] = bq.front();
bq.pop_front();
for (long long _ = 0; _ < 4; _++)
{
long long xx = x + dx[_], yy = y + dy[_];
if (!inside(xx, yy) || near[xx][yy] != INF)
{
continue;
}
near[xx][yy] = near[x][y] + 1;
bq.push_back({ xx, yy });
}
}
vector<vector<vector<long long>>> dist(n, vector<vector<long long>>(m, vector<long long>(5, INF)));
dist[v[0].first][v[0].second][4] = 0;
set<tuple<long long, long long, long long, long long>> dq = { { 0, v[0].first, v[0].second, 4 } };
while (dq.size())
{
auto [d, x, y, dir] = *dq.begin();
dq.erase(dq.begin());
if (dir == 4)
{
for (long long _ = 0; _ < 4; _++)
{
long long xx = x + dx[_], yy = y + dy[_];
if (!inside(xx, yy))
{
continue;
}
if (d + a + b < dist[xx][yy][_])
{
dq.erase({ dist[xx][yy][_], xx, yy, _ });
dist[xx][yy][_] = d + a + b;
dq.insert({ dist[xx][yy][_], xx, yy, _ });
}
long long neutr = min(d + a + b + near[xx][yy] * c, d + c);
if (neutr < dist[xx][yy][4])
{
dq.erase({ dist[xx][yy][4], xx, yy, 4 });
dist[xx][yy][4] = neutr;
dq.insert({ dist[xx][yy][4], xx, yy, 4 });
}
}
continue;
}
if (d + near[x][y] * c < dist[x][y][4])
{
dq.erase({ dist[x][y][4], x, y, 4 });
dist[x][y][4] = d + near[x][y] * c;
dq.insert({ dist[x][y][4], x, y, 4 });
}
long long xx = x + dx[dir], yy = y + dy[dir];
if (!inside(xx, yy))
{
continue;
}
if (d + a < dist[xx][yy][dir])
{
dq.erase({ dist[xx][yy][dir], xx, yy, dir });
dist[xx][yy][dir] = d + a;
dq.insert({ dist[xx][yy][dir], xx, yy, dir });
}
}
cout << dist[v[k - 1].first][v[k - 1].second][4] << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
srand(time(NULL));
int tst = 1;
//cin >> tst;
while (tst--)
{
solve();
}
return 0;
}
/*
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠤⠖⠚⢉⣩⣭⡭⠛⠓⠲⠦⣄⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⢀⡴⠋⠁⠀⠀⠊⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠳⢦⡀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠃⢀⡴⢳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀
⠀⠀⠀⠀⡾⠁⣠⠋⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀
⠀⠀⠀⣸⠁⢰⠃⠀⠀⠀⠈⢣⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣇⠀
⠀⠀⠀⡇⠀⡾⡀⠀⠀⠀⠀⣀⣹⣆⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⠀
⠀⠀⢸⠃⢀⣇⡈⠀⠀⠀⠀⠀⠀⢀⡑⢄⡀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⢸⠀⢻⡟⡻⢶⡆⠀⠀⠀⠀⡼⠟⡳⢿⣦⡑⢄⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇
⠀⠀⣸⠀⢸⠃⡇⢀⠇⠀⠀⠀⠀⠀⡼⠀⠀⠈⣿⡗⠂⠀⠀⠀⠀⠀⠀⠀⢸⠁
⠀⠀⡏⠀⣼⠀⢳⠊⠀⠀⠀⠀⠀⠀⠱⣀⣀⠔⣸⠁⠀⠀⠀⠀⠀⠀⠀⢠⡟⠀
⠀⠀⡇⢀⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⠀⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⠃⠀
⠀⢸⠃⠘⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠁⠀⠀⢀⠀⠀⠀⠀⠀⣾⠀⠀
⠀⣸⠀⠀⠹⡄⠀⠀⠈⠁⠀⠀⠀⠀⠀⠀⠀⡞⠀⠀⠀⠸⠀⠀⠀⠀⠀⡇⠀⠀
⠀⡏⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⢀⣠⢶⡇⠀⠀⢰⡀⠀⠀⠀⠀⠀⡇⠀⠀
⢰⠇⡄⠀⠀⠀⡿⢣⣀⣀⣀⡤⠴⡞⠉⠀⢸⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⣧⠀⠀
⣸⠀⡇⠀⠀⠀⠀⠀⠀⠉⠀⠀⠀⢹⠀⠀⢸⠀⠀⢀⣿⠇⠀⠀⠀⠁⠀⢸⠀⠀
⣿⠀⡇⠀⠀⠀⠀⠀⢀⡤⠤⠶⠶⠾⠤⠄⢸⠀⡀⠸⣿⣀⠀⠀⠀⠀⠀⠈⣇⠀
⡇⠀⡇⠀⠀⡀⠀⡴⠋⠀⠀⠀⠀⠀⠀⠀⠸⡌⣵⡀⢳⡇⠀⠀⠀⠀⠀⠀⢹⡀
⡇⠀⠇⠀⠀⡇⡸⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠮⢧⣀⣻⢂⠀⠀⠀⠀⠀⠀⢧
⣇⠀⢠⠀⠀⢳⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⡎⣆⠀⠀⠀⠀⠀⠘
⢻⠀⠈⠰⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⠘⢮⣧⡀⠀⠀⠀⠀
⠸⡆⠀⠀⠇⣾⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠆⠀⠀⠀⠀⠀⠀⠀⠙⠳⣄⡀⢢⡀
<3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3 <3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
7908 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
415 ms |
31760 KB |
Output is correct |
4 |
Correct |
402 ms |
32956 KB |
Output is correct |
5 |
Correct |
64 ms |
7772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
384 ms |
36632 KB |
Output is correct |
2 |
Correct |
411 ms |
37612 KB |
Output is correct |
3 |
Correct |
265 ms |
28452 KB |
Output is correct |
4 |
Correct |
234 ms |
30456 KB |
Output is correct |
5 |
Correct |
280 ms |
28444 KB |
Output is correct |
6 |
Correct |
268 ms |
35328 KB |
Output is correct |
7 |
Correct |
450 ms |
39372 KB |
Output is correct |
8 |
Correct |
353 ms |
39200 KB |
Output is correct |
9 |
Correct |
431 ms |
38988 KB |
Output is correct |
10 |
Correct |
52 ms |
6912 KB |
Output is correct |
11 |
Correct |
475 ms |
39272 KB |
Output is correct |
12 |
Correct |
421 ms |
38424 KB |
Output is correct |
13 |
Correct |
202 ms |
29392 KB |
Output is correct |
14 |
Correct |
440 ms |
39200 KB |
Output is correct |
15 |
Correct |
307 ms |
31468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
7908 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
415 ms |
31760 KB |
Output is correct |
4 |
Correct |
402 ms |
32956 KB |
Output is correct |
5 |
Correct |
64 ms |
7772 KB |
Output is correct |
6 |
Correct |
384 ms |
36632 KB |
Output is correct |
7 |
Correct |
411 ms |
37612 KB |
Output is correct |
8 |
Correct |
265 ms |
28452 KB |
Output is correct |
9 |
Correct |
234 ms |
30456 KB |
Output is correct |
10 |
Correct |
280 ms |
28444 KB |
Output is correct |
11 |
Correct |
268 ms |
35328 KB |
Output is correct |
12 |
Correct |
450 ms |
39372 KB |
Output is correct |
13 |
Correct |
353 ms |
39200 KB |
Output is correct |
14 |
Correct |
431 ms |
38988 KB |
Output is correct |
15 |
Correct |
52 ms |
6912 KB |
Output is correct |
16 |
Correct |
475 ms |
39272 KB |
Output is correct |
17 |
Correct |
421 ms |
38424 KB |
Output is correct |
18 |
Correct |
202 ms |
29392 KB |
Output is correct |
19 |
Correct |
440 ms |
39200 KB |
Output is correct |
20 |
Correct |
307 ms |
31468 KB |
Output is correct |
21 |
Correct |
73 ms |
9804 KB |
Output is correct |
22 |
Correct |
584 ms |
30516 KB |
Output is correct |
23 |
Correct |
536 ms |
26528 KB |
Output is correct |
24 |
Correct |
549 ms |
27512 KB |
Output is correct |
25 |
Correct |
545 ms |
34816 KB |
Output is correct |
26 |
Correct |
499 ms |
30768 KB |
Output is correct |
27 |
Correct |
219 ms |
22032 KB |
Output is correct |
28 |
Correct |
343 ms |
22824 KB |
Output is correct |
29 |
Correct |
486 ms |
28376 KB |
Output is correct |
30 |
Correct |
241 ms |
22468 KB |
Output is correct |
31 |
Correct |
548 ms |
39292 KB |
Output is correct |
32 |
Correct |
599 ms |
38224 KB |
Output is correct |
33 |
Correct |
339 ms |
35168 KB |
Output is correct |
34 |
Correct |
691 ms |
34592 KB |
Output is correct |
35 |
Correct |
217 ms |
22704 KB |
Output is correct |