Submission #1013294

#TimeUsernameProblemLanguageResultExecution timeMemory
1013294snpmrnhlolSoccer (JOI17_soccer)C++17
100 / 100
207 ms22356 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 501;
const ll K = 1e5;
const ll inf = 1e18;
struct point{
    ll x,y;
};
ll dist[N][N][3];
ll dist2[N][N];
bool vis[N][N];
point player[K];
priority_queue <array<ll,4>> pq;
queue <array<ll,2>> q;
ll d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
ll n,m,a,b,c;
ll k;
bool inbound(ll x,ll y){
    return 0 <= x && x <= n && 0 <= y && y <= m;
}
void add(ll nr,ll x,ll y,ll id){
    if(dist[x][y][id] > nr){
        dist[x][y][id] = nr;
        pq.push({-nr,x,y,id});
    }
}
void bfs(){
    for(ll i = 0;i < k;i++){
        q.push({player[i].x,player[i].y});
        vis[player[i].x][player[i].y] = 1;
        dist2[player[i].x][player[i].y] = 0;
    }
    while(!q.empty()){
        ll x = q.front()[0];
        ll y = q.front()[1];
        q.pop();
        for(ll l = 0;l < 4;l++){
            ll nx = x + d[l][0];
            ll ny = y + d[l][1];
            if(inbound(nx,ny) && !vis[nx][ny]){
                vis[nx][ny] = 1;
                dist2[nx][ny] = dist2[x][y] + 1;
                q.push({nx,ny});
            }
        }
    }
}
int main(){
    cin>>n>>m;
    cin>>a>>b>>c;
    cin>>k;
    for(ll i = 0;i < k;i++){
        cin>>player[i].x>>player[i].y;
    }
    for(ll i = 0;i <= n;i++){
        for(ll j = 0;j <= m;j++){
            for(ll l = 0;l < 3;l++){
                dist[i][j][l] = inf;
            }
        }
    }
    bfs();
    pq.push({0,player[0].x,player[0].y,0});
    dist[player[0].x][player[0].y][0] = 0;
    ///0 - on player
    ///1 - thrown left/right
    ///2 - thrown up/down
    while(!pq.empty()){
        auto x = pq.top();
        x[0] = -x[0];
        pq.pop();
        if(x[0] != dist[x[1]][x[2]][x[3]]){
            continue;
        }
        if(x[3] == 0){
            ///throw ball
            add(x[0] + b,x[1],x[2],1);
            add(x[0] + b,x[1],x[2],2);
            ///directional movement
            for(ll l = 0;l < 4;l++){
                ll nx = x[1] + d[l][0];
                ll ny = x[2] + d[l][1];
                if(inbound(nx,ny)){
                    add(x[0] + c,nx,ny,x[3]);
                }
            }
        }else if(x[3] == 1){
            ///catch
            add(x[0] + dist2[x[1]][x[2]]*c,x[1],x[2],0);
            ///directional movement
            for(ll l = 2;l < 4;l++){
                ll nx = x[1] + d[l][0];
                ll ny = x[2] + d[l][1];
                if(inbound(nx,ny)){
                    add(x[0] + a,nx,ny,x[3]);
                }
            }
        }else{
            ///catch
            add(x[0] + dist2[x[1]][x[2]]*c,x[1],x[2],0);
            ///directional movement
            for(ll l = 0;l < 2;l++){
                ll nx = x[1] + d[l][0];
                ll ny = x[2] + d[l][1];
                if(inbound(nx,ny)){
                    add(x[0] + a,nx,ny,x[3]);
                }
            }
        }
    }
    /*for(ll i = 0;i <= n;i++){
        for(ll j = 0;j <= m;j++){
            cout<<dist[i][j][0]<<' ';
        }
        cout<<'\n';
    }*/
    cout<<dist[player[k - 1].x][player[k - 1].y][0]<<'\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...