Submission #46666

#TimeUsernameProblemLanguageResultExecution timeMemory
46666qoo2p5Soccer (JOI17_soccer)C++17
100 / 100
697 ms39736 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = (int) 1e9 + 1e6 + 123;
const ll LINF = (ll) 1e18 + 1e9 + 123;

#define rep(i, s, t) for (auto i = (s); i < (t); ++(i))
#define per(i, s, t) for (auto i = (s); i >= (t); --(i))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define mp make_pair
#define pb push_back

bool mini(auto &x, const auto &y) {
    if (y < x) {
        x = y;
        return 1;
    }
    return 0;
}

bool maxi(auto &x, const auto &y) {
    if (y > x) {
        x = y;
        return 1;
    }
    return 0;
}

void run();

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    run();
    return 0;
}

const int N = (int) 1e5 + 123;
const int K = 505;

int h, w;
ll a, b, c;
int n;
int x[N], y[N];
int dist[K][K];
ll dp[K][K][6];
bool vis[K][K][6];

const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};

struct V {
    int i, j, t;
    
    bool operator<(const V &b) const {
        return mp(i, mp(j, t)) < mp(b.i, mp(b.j, b.t));
    }
};

void run() {
    cin >> h >> w;
    cin >> a >> b >> c;
    cin >> n;
    rep(i, 0, n) {
        cin >> x[i] >> y[i];
        x[i]++;
        y[i]++;
    }
    
    {
        queue<pair<int, int>> q;
        rep(i, 0, K) rep(j, 0, K) dist[i][j] = INF;
        rep(i, 0, n) {
            q.push({x[i], y[i]});
            dist[x[i]][y[i]] = 0;
        }
        while (sz(q)) {
            auto v = q.front();
            q.pop();
            rep(dir, 0, 4) {
                int nx = v.first + dx[dir];
                int ny = v.second + dy[dir];
                if (0 <= nx && nx < K && 0 <= ny && ny < K && mini(dist[nx][ny], dist[v.first][v.second] + 1)) {
                    q.push({nx, ny});
                }
            }
        }
    }
    
    rep(i, 1, K - 1) {
        rep(j, 1, K - 1) {
            
        }
    }
    
    rep(i, 0, K) rep(j, 0, K) rep(t, 0, 6) {
        dp[i][j][t] = LINF * (i != 0 && i != K - 1) * (j != 0 && j != K - 1);
    }
    dp[x[0]][y[0]][1] = 0;
    priority_queue<pair<ll, V>> q;
    q.push({0, {x[0], y[0], 1}});
    
    while (sz(q)) {
        V v = q.top().second;
        q.pop();
        if (vis[v.i][v.j][v.t]) {
            continue;
        }
        vis[v.i][v.j][v.t] = 1;
        
        if (v.t == 0) {
            if (mini(dp[v.i][v.j][1], dp[v.i][v.j][0] + dist[v.i][v.j] * c)) {
                q.push({-dp[v.i][v.j][1], {v.i, v.j, 1}});
            }
        } else if (v.t == 1) {
            rep(dir, 0, 4) {
                if (mini(dp[v.i][v.j][2 + dir], dp[v.i][v.j][1] + b)) {
                    q.push({-dp[v.i][v.j][2 + dir], {v.i, v.j, 2 + dir}});
                }
                int ni = v.i + dx[dir];
                int nj = v.j + dy[dir];
                if (mini(dp[ni][nj][1], dp[v.i][v.j][1] + c)) {
                    q.push({-dp[ni][nj][1], {ni, nj, 1}});
                }
            }
            if (mini(dp[v.i][v.j][0], dp[v.i][v.j][v.t])) {
                q.push({-dp[v.i][v.j][0], {v.i, v.j, 0}});
            }
        } else {
            int dir = v.t - 2;
            int ni = v.i + dx[dir];
            int nj = v.j + dy[dir];
            if (mini(dp[ni][nj][v.t], dp[v.i][v.j][v.t] + a)) {
                q.push({-dp[ni][nj][v.t], {ni, nj, v.t}});
            }
            if (mini(dp[v.i][v.j][0], dp[v.i][v.j][v.t])) {
                q.push({-dp[v.i][v.j][0], {v.i, v.j, 0}});
            }
        }
    }
    
    cout << dp[x[n - 1]][y[n - 1]][0] << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...