This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |