#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"
using ll = long long;
using ld = long double;
template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}
const int N = 251e3 + 5;
const ll INF = 1e17;
struct Node{
int x, dir; ll w; bool isPlayer;
Node(int x = 0, int dir = 0, ll w = 0, bool isPlayer = false): x(x), dir(dir), w(w), isPlayer(isPlayer) {}
bool operator < (const Node& q) const{
return w > q.w;
}
};
vector<vector<ll>> dist[N];
void Main()
{
int H,W; cin >> H >> W;
vector<vector<bool>> has(H + 1, vector<bool>(W + 1, false));
auto id = [&](int r, int c) -> int{
return r * (W + 1) + c;
};
auto init = [&](int x, int y) -> bool{
return (0 <= x && x <= H) && (0 <= y && y <= W);
};
ll A,B,C; cin >> A >> B >> C;
int n, beg, fin; cin >> n;
FOR(i, 1, n){
int x,y; cin >> x >> y;
has[x][y] = true;
if(i == 1) beg = id(x, y);
if(i == n) fin = id(x, y);
}
// if A * p + B <= C * p -> A < C
auto hamat = [&]() -> ll{
int x1 = beg / (W + 1);
int x2 = fin / (W + 1);
int y1 = beg % (W + 1);
int y2 = fin % (W + 1);
return 1ll * (abs(x1 - x2) + abs(y1 - y2)) * C;
};
if(A >= C){
cout << hamat() << el;
return;
}
queue<pi> q; vector<ll> near(id(H, W) + 1, INF);
FOR(i, 0, H){
FOR(j, 0, W){
if(id(i, j) != beg) dist[id(i, j)] = vector<vector<ll>>(2, vector<ll>(2, INF));
else{
dist[id(i, j)] = vector<vector<ll>>(2, vector<ll>(2, 0));
dist[id(i, j)][0][0] = dist[id(i, j)][0][1] = INF;
}
if(has[i][j]){
q.emplace(i, j);
near[id(i, j)] = 0;
}
}
}
// 0: horizontal, 1: veritcal
const int dx[4] = {0, -1, 0, 1};
const int dy[4] = {1, 0, -1, 0};
while(sz(q)){
pi e = q.front(); q.pop();
int x = e.fi, y = e.se;
FOR(i, 0, 3){
int xx = x + dx[i];
int yy = y + dy[i];
if(init(xx, yy)){
if(near[id(xx, yy)] == INF){
near[id(xx, yy)] = near[id(x, y)] + C;
q.emplace(xx, yy);
}
}
}
}
ll kickDist = B / (C - A); // kick * A + B < C * kick => B / (C - A) < kick
priority_queue<Node> pq;
FOR(dir, 0, 1) pq.emplace(beg, dir, 0, true);
while(sz(pq)){
Node eq = pq.top(); pq.pop();
int x = eq.x, dir = eq.dir, isP = eq.isPlayer;
if(eq.w > dist[x][isP][dir]) continue;
if(!isP){
// move to the next position with the same direction
int r = x / (W + 1), c = x % (W + 1);
FOR(i, 0, 3){
if(dir == (i & 1)){
int nr = r + dx[i];
int nc = c + dy[i];
if(init(nr, nc)){
int v = id(nr, nc);
// the ball will be move
if(ckmn(dist[v][isP][dir], dist[x][isP][dir] + A)){
pq.emplace(v, dir, dist[v][isP][dir], isP);
}
// we can give it to a player or a player will come there and claim it
if(ckmn(dist[v][true][dir], dist[v][isP][dir] + near[v])){
pq.emplace(v, dir, dist[v][true][dir], true);
}
}
}
}
}
else{
// move to any direction because we are player
int r = x / (W + 1), c = x % (W + 1);
FOR(i, 0, 3){
int nr = r + dx[i];
int nc = c + dy[i];
// we will go to the position
if(init(nr, nc)){
int v = id(nr, nc);
if(ckmn(dist[v][isP][(i & 1)], dist[x][isP][dir] + C)){
pq.emplace(v, (i & 1), dist[v][isP][(i & 1)], isP);
}
}
nr = r + kickDist * dx[i];
nc = c + kickDist * dy[i];
// we will kick it to the position
if(init(nr, nc)){
int v = id(nr, nc);
// no one will receive it
if(ckmn(dist[v][false][(i & 1)], dist[x][isP][dir] + kickDist * A + B)){
pq.emplace(v, (i & 1), dist[v][false][(i & 1)], false);
}
// we will pass to some player
if(ckmn(dist[v][isP][(i & 1)], dist[x][isP][dir] + kickDist * A + B + near[v])){
pq.emplace(v, (i & 1), dist[v][isP][(i & 1)], isP);
}
}
}
}
}
ll ans = min(dist[fin][1][0], dist[fin][1][1]); // we need to have someone at finish position
cout << ans << el;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int t = 1; while(t--) Main();
return 0;
}
Compilation message (stderr)
soccer.cpp: In function 'int32_t main()':
soccer.cpp:210:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
210 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
soccer.cpp:211:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
211 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |