#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"
const ld PI = 3.1415926535;
const int N = 500+10;
const int M = 7e6 + 1;
int mod = 1e9+7;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 2;
int mult(int a, int b) {
return a * 1LL * b % mod;
}
int sum(int a, int b) {
if (a + b < 0)
return a + b + mod;
if (a + b >= mod)
return a + b - mod;
return a + b;
}
ll binpow(ll a, ll n) {
if (n == 0)
return 1;
if (n % 2 == 1) {
return binpow(a, n - 1) * a % mod;
} else {
ll b = binpow(a, n / 2);
return b * b % mod;
}
}
int di[] = {0, 0, -1, 1};
int dj[] = {1, -1, 0, 0};
int H, W, n;
ll a, b, c, mnd[N][N], used[N][N], d[N][N][3];
pii ps[100005];
void dijkstra(int si, int sj){
for(int i = 0; i <= H; i++)
for(int j = 0; j <= W; j++)
d[i][j][0] = d[i][j][1] = d[i][j][2] = infl;
d[si][sj][0] = 0;
priority_queue<pair<ll, pair<pii, int>>> st;
st.push({0LL, {{si, sj}, 0}});
while(!st.empty()){
int vi = st.top().S.F.F, vj = st.top().S.F.S, tp = st.top().S.S;
if(-st.top().F != d[vi][vj][tp]){
st.pop();
continue;
}
st.pop();
if(tp == 0){
for(int dir = 0; dir < 4; dir++){
int ni = vi + di[dir], nj = vj + dj[dir];
if(ni >= 0 && ni <= H && nj >= 0 && nj <= W && d[ni][nj][0] > d[vi][vj][tp] + c){
d[ni][nj][0] = d[vi][vj][tp] + c;
st.push({-d[ni][nj][0], {{ni, nj}, 0}});
}
}
if(d[vi][vj][1] > d[vi][vj][0] + b){
d[vi][vj][1] = d[vi][vj][0] + b;
st.push({-d[vi][vj][1], {{vi, vj}, 1}});
}
if(d[vi][vj][2] > d[vi][vj][0] + b){
d[vi][vj][2] = d[vi][vj][0] + b;
st.push({-d[vi][vj][2], {{vi, vj}, 2}});
}
}else if(tp == 1){
for(int dir = 2; dir < 4; dir++){
int ni = vi + di[dir], nj = vj + dj[dir];
if(ni >= 0 && ni <= H && nj >= 0 && nj <= W && d[ni][nj][1] > d[vi][vj][1] + a){
d[ni][nj][1] = d[vi][vj][tp] + a;
st.push({-d[ni][nj][1], {{ni, nj}, 1}});
}
}
if(d[vi][vj][0] > d[vi][vj][tp] + mnd[vi][vj] * c){
d[vi][vj][0] = d[vi][vj][tp] + mnd[vi][vj] * c;
st.push({-d[vi][vj][0], {{vi, vj}, 0}});
}
}else{
for(int dir = 0; dir < 2; dir++){
int ni = vi + di[dir], nj = vj + dj[dir];
if(ni >= 0 && ni <= H && nj >= 0 && nj <= W && d[ni][nj][tp] > d[vi][vj][tp] + a){
d[ni][nj][tp] = d[vi][vj][tp] + a;
st.push({-d[ni][nj][tp], {{ni, nj}, tp}});
}
}
if(d[vi][vj][0] > d[vi][vj][tp] + mnd[vi][vj] * c){
d[vi][vj][0] = d[vi][vj][tp] + mnd[vi][vj] * c;
st.push({-d[vi][vj][0], {{vi, vj}, 0}});
}
}
}
}
void solve(){
cin >> H >> W;
cin >> a >> b >> c;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> ps[i].F >> ps[i].S;
}
queue<pii> q;
for(int i = 1; i <= n; i++){
q.push({ps[i].F, ps[i].S});
used[ps[i].F][ps[i].S] = 1;
}
while(!q.empty()){
int vi = q.front().F, vj = q.front().S;
q.pop();
for(int dir = 0; dir < 4; dir++){
int ni = vi + di[dir], nj = vj + dj[dir];
if(ni >= 0 && ni <= H && nj >= 0 && nj <= W && !used[ni][nj]){
used[ni][nj] = 1;
mnd[ni][nj] = mnd[vi][vj] + 1;
q.push({ni, nj});
}
}
}
dijkstra(ps[1].F, ps[1].S);
cout << d[ps[n].F][ps[n].S][0] << '\n';
}
signed main() {
mispertion;
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
13264 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
143 ms |
21200 KB |
Output is correct |
4 |
Correct |
149 ms |
21232 KB |
Output is correct |
5 |
Correct |
32 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
21696 KB |
Output is correct |
2 |
Correct |
145 ms |
20492 KB |
Output is correct |
3 |
Correct |
109 ms |
20176 KB |
Output is correct |
4 |
Correct |
97 ms |
20680 KB |
Output is correct |
5 |
Correct |
110 ms |
21696 KB |
Output is correct |
6 |
Correct |
98 ms |
21448 KB |
Output is correct |
7 |
Correct |
143 ms |
22204 KB |
Output is correct |
8 |
Correct |
137 ms |
20736 KB |
Output is correct |
9 |
Correct |
147 ms |
21188 KB |
Output is correct |
10 |
Correct |
23 ms |
14288 KB |
Output is correct |
11 |
Correct |
143 ms |
21668 KB |
Output is correct |
12 |
Correct |
148 ms |
20416 KB |
Output is correct |
13 |
Correct |
82 ms |
20692 KB |
Output is correct |
14 |
Correct |
141 ms |
22232 KB |
Output is correct |
15 |
Correct |
111 ms |
20928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
13264 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
143 ms |
21200 KB |
Output is correct |
4 |
Correct |
149 ms |
21232 KB |
Output is correct |
5 |
Correct |
32 ms |
11356 KB |
Output is correct |
6 |
Correct |
148 ms |
21696 KB |
Output is correct |
7 |
Correct |
145 ms |
20492 KB |
Output is correct |
8 |
Correct |
109 ms |
20176 KB |
Output is correct |
9 |
Correct |
97 ms |
20680 KB |
Output is correct |
10 |
Correct |
110 ms |
21696 KB |
Output is correct |
11 |
Correct |
98 ms |
21448 KB |
Output is correct |
12 |
Correct |
143 ms |
22204 KB |
Output is correct |
13 |
Correct |
137 ms |
20736 KB |
Output is correct |
14 |
Correct |
147 ms |
21188 KB |
Output is correct |
15 |
Correct |
23 ms |
14288 KB |
Output is correct |
16 |
Correct |
143 ms |
21668 KB |
Output is correct |
17 |
Correct |
148 ms |
20416 KB |
Output is correct |
18 |
Correct |
82 ms |
20692 KB |
Output is correct |
19 |
Correct |
141 ms |
22232 KB |
Output is correct |
20 |
Correct |
111 ms |
20928 KB |
Output is correct |
21 |
Correct |
33 ms |
11608 KB |
Output is correct |
22 |
Correct |
181 ms |
22204 KB |
Output is correct |
23 |
Correct |
165 ms |
16332 KB |
Output is correct |
24 |
Correct |
185 ms |
17736 KB |
Output is correct |
25 |
Correct |
155 ms |
22244 KB |
Output is correct |
26 |
Correct |
174 ms |
22108 KB |
Output is correct |
27 |
Correct |
142 ms |
14552 KB |
Output is correct |
28 |
Correct |
119 ms |
14824 KB |
Output is correct |
29 |
Correct |
173 ms |
20300 KB |
Output is correct |
30 |
Correct |
102 ms |
14680 KB |
Output is correct |
31 |
Correct |
142 ms |
20936 KB |
Output is correct |
32 |
Correct |
176 ms |
23620 KB |
Output is correct |
33 |
Correct |
132 ms |
20676 KB |
Output is correct |
34 |
Correct |
167 ms |
21340 KB |
Output is correct |
35 |
Correct |
95 ms |
14408 KB |
Output is correct |