#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pii pair<int,int>
#define PII pair<pii,pii>
using namespace std;
const int N = 505 + 5, inf = 1e18; // !
int d[N][N][2], D[N][N][6];int n, m;
int dx[] = {0, 0, 0, 1, -1}, dy[] = {0, 1, -1, 0, 0};
bool ok(int i,int j) {
return (min(i, j) >= 0 && i <= n && j <= m);
}
main(){
cin >> n >> m;
int a, b, c;
cin >> a >> b >> c;
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= m; j++) {
d[i][j][0] = d[i][j][1] = inf;
for(int t = 0; t <= 5; ++t) D[i][j][t] = inf;
}
}
int Q;
cin >> Q;
priority_queue<PII, vector<PII>, greater<PII> > pq;
queue<pair<int,int>> q[2];
for(int i = 1; i <= Q; i++) {
int x, y; cin >> x >> y;
if(d[x][y][0])
q[0].push({x, y});
d[x][y][0] = 0;
if(i == 1) {
D[x][y][0] = 0;
pq.push({{0, 0}, {x, y}});
}
if(i == Q) {
d[x][y][1] = 0;
q[1].push({x, y});
}
}
for(int t = 0; t < 2; t++)
while(q[t].size()) {
int x = q[t].front().f, y = q[t].front().s;
q[t].pop();
for(int j = 1; j <= 4; j++) {
int xn = x + dx[j], yn = y + dy[j];
if(ok(xn, yn) && d[xn][yn][t] > d[x][y][t] + c) {
d[xn][yn][t] = d[x][y][t] + c;
q[t].push({xn, yn});
}
}
}
int ans = 1e18;
while(pq.size()) {
int di = pq.top().f.f, t = pq.top().f.s;
int x = pq.top().s.f, y = pq.top().s.s;
pq.pop();
if(D[x][y][t] < di) continue;
ans = min(ans, D[x][y][t] + d[x][y][1]);
if(t && t < 5) {
int xn = x +dx[t], yn = y + dy[t];
if(ok(xn, yn) && D[xn][yn][t] > di + a) {
D[xn][yn][t] = di + a;
pq.push({{di + a, t}, {xn, yn}});
}
if(D[x][y][5] > di) {
D[x][y][5] = di;
pq.push({{di, 5}, {x, y}});
}
continue;
}
if(t == 5) {
if(D[x][y][0] > D[x][y][5] + d[x][y][0]) {
D[x][y][0] = D[x][y][5] + d[x][y][0];
pq.push({{D[x][y][0], 0}, {x, y}});
}
continue;
}
for(int t = 1; t < 5; t++) {
int xn = x + dx[t], yn = y + dy[t];
if(!ok(xn, yn)) continue;
if(D[xn][yn][0] > di + c) {
D[xn][yn][0] = di + c;
pq.push({{di + c, 0}, {xn, yn}});
}
if(D[xn][yn][t] > di + a + b) {
D[xn][yn][t] = di + a + b;
pq.push({{di + a + b, t}, {xn, yn}});
}
}
}
cout << ans;
}
Compilation message
soccer.cpp:14:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
14 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
8564 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
309 ms |
24536 KB |
Output is correct |
4 |
Correct |
342 ms |
24624 KB |
Output is correct |
5 |
Correct |
89 ms |
9200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
373 ms |
24568 KB |
Output is correct |
2 |
Correct |
370 ms |
24648 KB |
Output is correct |
3 |
Correct |
270 ms |
21424 KB |
Output is correct |
4 |
Correct |
279 ms |
24652 KB |
Output is correct |
5 |
Correct |
279 ms |
24052 KB |
Output is correct |
6 |
Correct |
296 ms |
24624 KB |
Output is correct |
7 |
Correct |
367 ms |
24752 KB |
Output is correct |
8 |
Correct |
329 ms |
24768 KB |
Output is correct |
9 |
Correct |
390 ms |
24864 KB |
Output is correct |
10 |
Correct |
55 ms |
9100 KB |
Output is correct |
11 |
Correct |
375 ms |
24760 KB |
Output is correct |
12 |
Correct |
367 ms |
24676 KB |
Output is correct |
13 |
Correct |
229 ms |
21420 KB |
Output is correct |
14 |
Correct |
371 ms |
24740 KB |
Output is correct |
15 |
Correct |
293 ms |
24172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
8564 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
309 ms |
24536 KB |
Output is correct |
4 |
Correct |
342 ms |
24624 KB |
Output is correct |
5 |
Correct |
89 ms |
9200 KB |
Output is correct |
6 |
Correct |
373 ms |
24568 KB |
Output is correct |
7 |
Correct |
370 ms |
24648 KB |
Output is correct |
8 |
Correct |
270 ms |
21424 KB |
Output is correct |
9 |
Correct |
279 ms |
24652 KB |
Output is correct |
10 |
Correct |
279 ms |
24052 KB |
Output is correct |
11 |
Correct |
296 ms |
24624 KB |
Output is correct |
12 |
Correct |
367 ms |
24752 KB |
Output is correct |
13 |
Correct |
329 ms |
24768 KB |
Output is correct |
14 |
Correct |
390 ms |
24864 KB |
Output is correct |
15 |
Correct |
55 ms |
9100 KB |
Output is correct |
16 |
Correct |
375 ms |
24760 KB |
Output is correct |
17 |
Correct |
367 ms |
24676 KB |
Output is correct |
18 |
Correct |
229 ms |
21420 KB |
Output is correct |
19 |
Correct |
371 ms |
24740 KB |
Output is correct |
20 |
Correct |
293 ms |
24172 KB |
Output is correct |
21 |
Correct |
107 ms |
9364 KB |
Output is correct |
22 |
Correct |
446 ms |
24748 KB |
Output is correct |
23 |
Correct |
432 ms |
19152 KB |
Output is correct |
24 |
Correct |
477 ms |
20936 KB |
Output is correct |
25 |
Correct |
431 ms |
24640 KB |
Output is correct |
26 |
Correct |
470 ms |
24972 KB |
Output is correct |
27 |
Correct |
263 ms |
18924 KB |
Output is correct |
28 |
Correct |
301 ms |
19156 KB |
Output is correct |
29 |
Correct |
403 ms |
21976 KB |
Output is correct |
30 |
Correct |
271 ms |
18380 KB |
Output is correct |
31 |
Correct |
381 ms |
24752 KB |
Output is correct |
32 |
Correct |
501 ms |
25748 KB |
Output is correct |
33 |
Correct |
304 ms |
24604 KB |
Output is correct |
34 |
Correct |
514 ms |
24892 KB |
Output is correct |
35 |
Correct |
269 ms |
17688 KB |
Output is correct |