Submission #1306738

#TimeUsernameProblemLanguageResultExecution timeMemory
1306738nguyenkhangninh99Soccer (JOI17_soccer)C++20
100 / 100
356 ms30076 KiB
#include<bits/stdc++.h>
using namespace std;

#define int long long
const int maxn = 5e2 + 5, inf = 1e16;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    int h, w, a, b, c, n; cin >> h >> w >> a >> b >> c >> n;
    vector<vector<vector<int>>> res(h + 1, vector<vector<int>>(w + 1, vector<int>(5, inf)));
    vector<vector<int>> dist(h + 1, vector<int>(w + 1, inf));

    int startx, starty, endx, endy;
    queue<pair<int, int>> q;
    for(int i = 1; i <= n; i++){
        int x, y; cin >> x >> y;
        if(i == 1) startx = x, starty = y;
        if(i == n) endx = x, endy = y;
        dist[x][y] = 0;
        q.push({x, y});
    }

    auto inside = [&](int x, int y){
        return 0 <= x && x <= h && 0 <= y && y <= w;
    };
    auto minimize = [&](int &x, int y){
        if(x > y){
            x = y;
            return true;
        }return false;
    };
    while(!q.empty()){
        auto [x, y] = q.front();
        q.pop();
        for(int i = 0; i < 4; i++){
            int nx = x + dx[i], ny = y + dy[i];
            if(inside(nx, ny) && dist[nx][ny] == inf){
                dist[nx][ny] = dist[x][y] + 1;
                q.push({nx, ny});
            }
        }
    }

    priority_queue<array<int, 4>, vector<array<int, 4>>, greater<array<int, 4>>> pq;
    pq.push({0, startx, starty, 4});
    res[startx][starty][4] = 0;

    while(pq.size()){
        auto [d, x, y, m] = pq.top();
        pq.pop();

        if(d > res[x][y][m]) continue;
        if(m == 4){
            for(int i = 0; i < 4; i++){
                if(minimize(res[x][y][i], d + b)) pq.push({res[x][y][i], x, y, i}); //đá đi 1 trong 4 hướng
                int nx = x + dx[i], ny = y + dy[i];
                if(inside(nx, ny) && minimize(res[nx][ny][4], d + c)) pq.push({res[nx][ny][4], nx, ny, 4}); //đi 1 trong 4 hướng
            }
        }else{
            int nx = x + dx[m], ny = y + dy[m];
            if(inside(nx, ny) && minimize(res[nx][ny][m], d + a)) pq.push({res[nx][ny][m], nx, ny, m}); //bay tiếp
            if(minimize(res[x][y][4], d + dist[x][y] * c)) pq.push({res[x][y][4], x, y, 4}); //có thằng đến nhặt
        }
    }

    cout << res[endx][endy][4];
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...