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>
using namespace std;
#define f first
#define s second
#define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 1000, M = 1e5 + 5, inf = 1e18;
int t,n,a,b,c,h,w,x[M],y[M],dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0};
int dp[N][N][10],dist[N][N];
int check(int x, int y) {
return (!(x <= 0 || y <= 0 || x > h || y > w));
}
void bfs() {
queue <pii> q;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
dist[i][j] = inf;
}
}
for (int i = 1; i <= n; i++) {
dist[x[i]][y[i]] = 0;
q.push({x[i],y[i]});
}
while (!q.empty()) {
int x = q.front().f;
int y = q.front().s;
q.pop();
for (int i = 0; i <= 3; i++) {
int nx = x + dx[i]; int ny = y + dy[i];
if (check(nx,ny) && dist[nx][ny] == inf) {
dist[nx][ny] = dist[x][y] + 1;
q.push({nx,ny});
}
}
}
}
void dijkstra() {
priority_queue < pair <pii, pii > > q;
for (int i = 1; i <= h + 1; i++) {
for (int j = 1; j <= w + 1; j++ ){
for (int k = 0; k <= 4; k++) {
dp[i][j][k] = inf;
}
}
}
q.push({{0,4},{x[1],y[1]}});
dp[x[1]][y[1]][4] = 0;
while (!q.empty()) {
int dis = -q.top().f.f;
int dir = q.top().f.s;
int x = q.top().s.f;
int y = q.top().s.s;
q.pop();
if (dp[x][y][dir] < dis) continue;
if (dir == 4) {
for (int i = 0; i <= 3; i++) {
int nx = x + dx[i]; int ny = y + dy[i];
if (dp[x][y][i] > dp[x][y][4] + b) {
dp[x][y][i] = dp[x][y][4] + b;
q.push({{-dp[x][y][i], i},{x,y}});
}
if (check(nx,ny) && dp[nx][ny][i] > dp[x][y][dir] + b + c){
dp[nx][ny][i] = dp[x][y][dir] + b + c;
q.push({{-dp[nx][ny][i], i},{nx,ny}});
}
}
for (int i = 0; i <= 3; i++) {
int nx = x + dx[i]; int ny = y + dy[i];
if (check(nx,ny) && dp[nx][ny][4] > dp[x][y][dir] + c) {
dp[nx][ny][4] = dp[x][y][4] + c;
q.push({{-dp[nx][ny][4], 4},{nx,ny}});
}
}
}
else {
if (dp[x][y][4] > dp[x][y][dir] + c*dist[x][y]) {
dp[x][y][4] = dp[x][y][dir] + c*dist[x][y];
q.push({{-dp[x][y][4], 4},{x,y}});
}
int nx = x + dx[dir];
int ny = y + dy[dir];
//if (x == 4 && y == 6 && dir == 2) cout<<nx<<" "<<ny<<"\n";
if (check(nx,ny) && dp[nx][ny][dir] > dp[x][y][dir] + a) {
dp[nx][ny][dir] = dp[x][y][dir] + a;
q.push({{-dp[nx][ny][dir], dir},{nx,ny}});
}
}
}
int ans = inf;
cout<<dp[x[n]][y[n]][4]<<"\n";
}
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>h>>w; h++;w++;
cin>>a>>b>>c;
cin>>n;
for (int i = 1; i <= n; i++) {
cin>>x[i]>>y[i];
x[i]++;
y[i]++;
}
bfs();
dijkstra();
}
Compilation message (stderr)
soccer.cpp: In function 'void dijkstra()':
soccer.cpp:92:9: warning: unused variable 'ans' [-Wunused-variable]
92 | int ans = inf;
| ^~~
soccer.cpp: At global scope:
soccer.cpp:95:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
95 | main() {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |