# 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
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 |
1 |
Correct |
88 ms |
10192 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
349 ms |
34388 KB |
Output is correct |
4 |
Correct |
370 ms |
34276 KB |
Output is correct |
5 |
Correct |
74 ms |
11380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
391 ms |
34368 KB |
Output is correct |
2 |
Correct |
396 ms |
34360 KB |
Output is correct |
3 |
Correct |
273 ms |
29208 KB |
Output is correct |
4 |
Correct |
291 ms |
34296 KB |
Output is correct |
5 |
Correct |
288 ms |
30148 KB |
Output is correct |
6 |
Correct |
292 ms |
34372 KB |
Output is correct |
7 |
Correct |
375 ms |
34564 KB |
Output is correct |
8 |
Correct |
351 ms |
34364 KB |
Output is correct |
9 |
Correct |
407 ms |
42680 KB |
Output is correct |
10 |
Correct |
75 ms |
12272 KB |
Output is correct |
11 |
Correct |
364 ms |
34480 KB |
Output is correct |
12 |
Correct |
395 ms |
34324 KB |
Output is correct |
13 |
Correct |
224 ms |
29244 KB |
Output is correct |
14 |
Correct |
396 ms |
34532 KB |
Output is correct |
15 |
Correct |
283 ms |
30280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
10192 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
349 ms |
34388 KB |
Output is correct |
4 |
Correct |
370 ms |
34276 KB |
Output is correct |
5 |
Correct |
74 ms |
11380 KB |
Output is correct |
6 |
Correct |
391 ms |
34368 KB |
Output is correct |
7 |
Correct |
396 ms |
34360 KB |
Output is correct |
8 |
Correct |
273 ms |
29208 KB |
Output is correct |
9 |
Correct |
291 ms |
34296 KB |
Output is correct |
10 |
Correct |
288 ms |
30148 KB |
Output is correct |
11 |
Correct |
292 ms |
34372 KB |
Output is correct |
12 |
Correct |
375 ms |
34564 KB |
Output is correct |
13 |
Correct |
351 ms |
34364 KB |
Output is correct |
14 |
Correct |
407 ms |
42680 KB |
Output is correct |
15 |
Correct |
75 ms |
12272 KB |
Output is correct |
16 |
Correct |
364 ms |
34480 KB |
Output is correct |
17 |
Correct |
395 ms |
34324 KB |
Output is correct |
18 |
Correct |
224 ms |
29244 KB |
Output is correct |
19 |
Correct |
396 ms |
34532 KB |
Output is correct |
20 |
Correct |
283 ms |
30280 KB |
Output is correct |
21 |
Correct |
96 ms |
11388 KB |
Output is correct |
22 |
Correct |
481 ms |
34332 KB |
Output is correct |
23 |
Correct |
442 ms |
27704 KB |
Output is correct |
24 |
Correct |
517 ms |
30520 KB |
Output is correct |
25 |
Correct |
414 ms |
34460 KB |
Output is correct |
26 |
Correct |
447 ms |
34848 KB |
Output is correct |
27 |
Correct |
253 ms |
28964 KB |
Output is correct |
28 |
Correct |
290 ms |
29720 KB |
Output is correct |
29 |
Correct |
404 ms |
34028 KB |
Output is correct |
30 |
Correct |
301 ms |
29524 KB |
Output is correct |
31 |
Correct |
414 ms |
34524 KB |
Output is correct |
32 |
Correct |
545 ms |
37036 KB |
Output is correct |
33 |
Correct |
319 ms |
34304 KB |
Output is correct |
34 |
Correct |
508 ms |
34536 KB |
Output is correct |
35 |
Correct |
244 ms |
28872 KB |
Output is correct |