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>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
const ll INF = 1e18;
vector < vector <ll> > dst_c;
int dl[] = {-1, 0, 1, 0}, dc[] = {0, -1, 0, 1};
vector <int> x, y;
int h, w, A, B, C, n;
inline bool in(int l, int c) {
return l >= 0 && c >= 0 && l <= h && c <= w;
}
inline void bfs() {
queue < pair <int, int> > Q;
dst_c.resize(h + 1, vector <ll>(w + 1, INF));
for(int i = 1; i < n; i++) {
dst_c[x[i]][y[i]] = 0;
Q.push({x[i], y[i]});
}
while(Q.size()) {
auto cur = Q.front();
Q.pop();
for(int i = 0; i < 4; i++) {
int l = cur.first + dl[i];
int c = cur.second + dc[i];
if(in(l, c) && dst_c[l][c] == INF) {
dst_c[l][c] = dst_c[cur.first][cur.second] + C;
Q.push({l, c});
}
}
}
}
struct Data {
int l, c;
ll cst;
bool operator <(const Data &other) const {
return cst > other.cst;
}
};
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> h >> w >> A >> B >> C >> n;
x.resize(n + 1), y.resize(n + 1);
for(i = 1; i <= n; i++) {
cin >> x[i] >> y[i];
}
bfs();
vector < vector <ll> > dst(h + 1, vector <ll>(w + 1, INF));
vector < vector <bool> > vis(h + 1, vector <bool>(w + 1, 0));
priority_queue <Data> pq;
pq.push({x[1], y[1], 0});
dst[x[1]][y[1]] = 0;
while(pq.size()) {
auto cur = pq.top();
pq.pop();
if(vis[cur.l][cur.c]) {
continue;
}
vis[cur.l][cur.c] = 1;
for(i = 0; i < 4; i++) {
int l = cur.l + dl[i];
int c = cur.c + dc[i];
if(in(l, c) && dst[l][c] > cur.cst + C) {
dst[l][c] = cur.cst + C;
pq.push({l, c, dst[l][c]});
}
}
for(i = 0; i <= h; i++) {
ll cst = cur.cst + 1LL * A * abs(i - cur.l) + B;
if(i != x[n] || cur.c != y[n]) {
cst += dst_c[i][cur.c];
}
if(dst[i][cur.c] > cst) {
dst[i][cur.c] = cst;
pq.push({i, cur.c, cst});
}
}
for(i = 0; i <= w; i++) {
ll cst = cur.cst + 1LL * A * abs(i - cur.c) + B;
if(i != y[n] || cur.l != x[n]) {
cst += dst_c[cur.l][i];
}
if(dst[cur.l][i] > cst) {
dst[cur.l][i] = cst;
pq.push({cur.l, i, cst});
}
}
}
cout << dst[x[n]][y[n]];
//cin.close();
//cout.close();
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... |