# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1066335 |
2024-08-19T18:15:59 Z |
sammyuri |
Soccer (JOI17_soccer) |
C++17 |
|
226 ms |
17104 KB |
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dist[505][505];
ll dist_2[505][505][5];
ll a, b, c;
inline ll pack(pair<ll, pair<int, pair<int, int>>> xx) {
return -(xx.second.first + 5ll * (xx.first * 360000ll + (600ll * xx.second.second.first + xx.second.second.second)));
}
inline pair<ll, pair<int, pair<int, int>>> unpack(ll xx) {
xx = -xx;
int aa = xx % 5;
ll k = (xx / 5) % 360000ll;
return {xx / (5ll * 360000ll), {aa, {k / 600ll, k % 600ll}}};
}
priority_queue<ll> pq;
int ddx[] = {0, 0, 1, -1};
int ddy[] = {1, -1, 0, 0};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(dist, -1, sizeof(dist));
int h, w; cin >> h >> w;
h ++; w ++;
cin >> a >> b >> c;
vector<pair<int, int>> players;
int n; cin >> n;
int starti, startj, endi, endj;
for (int i = 0; i < n; i ++) {
int s, t; cin >> s >> t;
players.push_back({s, t});
if (i == 0)
starti = s, startj = t;
else if (i == n - 1)
endi = s, endj = t;
}
// multisource bfs
queue<pair<int, int>> q;
for (auto a : players) {
dist[a.first][a.second] = 0;
q.push(a);
}
while (q.size()) {
pair<int, int> cur = q.front(); q.pop();
int curdist = dist[cur.first][cur.second];
if (cur.first > 0 && dist[cur.first - 1][cur.second] == -1) {
q.push({cur.first - 1, cur.second});
dist[cur.first - 1][cur.second] = curdist + 1;
}
if (cur.first < h - 1 && dist[cur.first + 1][cur.second] == -1) {
q.push({cur.first + 1, cur.second});
dist[cur.first + 1][cur.second] = curdist + 1;
}
if (cur.second > 0 && dist[cur.first][cur.second - 1] == -1) {
q.push({cur.first, cur.second - 1});
dist[cur.first][cur.second - 1] = curdist + 1;
}
if (cur.second < w - 1 && dist[cur.first][cur.second + 1] == -1) {
q.push({cur.first, cur.second + 1});
dist[cur.first][cur.second + 1] = curdist + 1;
}
}
// calculate answer
for (int i = 0; i < h; i ++)
for (int j = 0; j < w; j ++) {
for (int k = 0; k < 5; k ++)
dist_2[i][j][k] = 1e18;
// initial distance
}
dist_2[starti][startj][4] = 0;
pq.push(pack({0, {4, {starti, startj}}}));
while (pq.size()) {
auto cur = unpack(pq.top()); pq.pop();
ll curdist = cur.first;
int dir = cur.second.first;
int i = cur.second.second.first, j = cur.second.second.second;
if (dist_2[i][j][dir] != curdist)
continue;
// cout << i << " " << j << " " << dir << " " << curdist << endl;
// catch
if (dir != 4) {
if (dist_2[i][j][4] > curdist + dist[i][j] * c) {
dist_2[i][j][4] = curdist + dist[i][j] * c;
pq.push(pack({curdist + dist[i][j] * c, {4, {i, j}}}));
}
int ii = i + ddx[dir], jj = j + ddy[dir];
if (ii >= 0 && jj >= 0 && ii < h && jj < w) {
if (dist_2[ii][jj][dir] > curdist + a) {
dist_2[ii][jj][dir] = curdist + a;
pq.push(pack({curdist + a, {dir, {ii, jj}}}));
}
}
}
// move
else {
for (int dd = 0; dd < 4; dd ++) {
int ii = i + ddx[dd], jj = j + ddy[dd];
if (ii >= 0 && jj >= 0 && ii < h && jj < w) {
if (dist_2[ii][jj][dd] > curdist + a + b) {
dist_2[ii][jj][dd] = curdist + a + b;
pq.push(pack({curdist + a + b, {dd, {ii, jj}}}));
}
if (dist_2[ii][jj][4] > curdist + c) {
dist_2[ii][jj][4] = curdist + c;
pq.push(pack({curdist + c, {4, {ii, jj}}}));
}
}
}
}
}
cout << dist_2[endi][endj][4] << endl;
return 0;
}
Compilation message
soccer.cpp: In function 'int main()':
soccer.cpp:12:112: warning: 'startj' may be used uninitialized in this function [-Wmaybe-uninitialized]
12 | return -(xx.second.first + 5ll * (xx.first * 360000ll + (600ll * xx.second.second.first + xx.second.second.second)));
| ~~~~~~~~~~~~~~~~~^~~~~~
soccer.cpp:34:17: note: 'startj' was declared here
34 | int starti, startj, endi, endj;
| ^~~~~~
soccer.cpp:12:87: warning: 'starti' may be used uninitialized in this function [-Wmaybe-uninitialized]
12 | return -(xx.second.first + 5ll * (xx.first * 360000ll + (600ll * xx.second.second.first + xx.second.second.second)));
| ~~~~~~~~~~~~~~~~~^~~~~
soccer.cpp:34:9: note: 'starti' was declared here
34 | int starti, startj, endi, endj;
| ^~~~~~
soccer.cpp:118:33: warning: 'endj' may be used uninitialized in this function [-Wmaybe-uninitialized]
118 | cout << dist_2[endi][endj][4] << endl;
| ^
soccer.cpp:118:33: warning: 'endi' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
6620 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
171 ms |
14364 KB |
Output is correct |
4 |
Correct |
183 ms |
14508 KB |
Output is correct |
5 |
Correct |
39 ms |
7516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
168 ms |
14556 KB |
Output is correct |
2 |
Correct |
150 ms |
14384 KB |
Output is correct |
3 |
Correct |
133 ms |
12556 KB |
Output is correct |
4 |
Correct |
116 ms |
14536 KB |
Output is correct |
5 |
Correct |
118 ms |
14532 KB |
Output is correct |
6 |
Correct |
125 ms |
14552 KB |
Output is correct |
7 |
Correct |
137 ms |
14612 KB |
Output is correct |
8 |
Correct |
152 ms |
14544 KB |
Output is correct |
9 |
Correct |
141 ms |
14536 KB |
Output is correct |
10 |
Correct |
24 ms |
6612 KB |
Output is correct |
11 |
Correct |
131 ms |
14572 KB |
Output is correct |
12 |
Correct |
176 ms |
14536 KB |
Output is correct |
13 |
Correct |
95 ms |
12504 KB |
Output is correct |
14 |
Correct |
134 ms |
14584 KB |
Output is correct |
15 |
Correct |
130 ms |
14532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
6620 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
171 ms |
14364 KB |
Output is correct |
4 |
Correct |
183 ms |
14508 KB |
Output is correct |
5 |
Correct |
39 ms |
7516 KB |
Output is correct |
6 |
Correct |
168 ms |
14556 KB |
Output is correct |
7 |
Correct |
150 ms |
14384 KB |
Output is correct |
8 |
Correct |
133 ms |
12556 KB |
Output is correct |
9 |
Correct |
116 ms |
14536 KB |
Output is correct |
10 |
Correct |
118 ms |
14532 KB |
Output is correct |
11 |
Correct |
125 ms |
14552 KB |
Output is correct |
12 |
Correct |
137 ms |
14612 KB |
Output is correct |
13 |
Correct |
152 ms |
14544 KB |
Output is correct |
14 |
Correct |
141 ms |
14536 KB |
Output is correct |
15 |
Correct |
24 ms |
6612 KB |
Output is correct |
16 |
Correct |
131 ms |
14572 KB |
Output is correct |
17 |
Correct |
176 ms |
14536 KB |
Output is correct |
18 |
Correct |
95 ms |
12504 KB |
Output is correct |
19 |
Correct |
134 ms |
14584 KB |
Output is correct |
20 |
Correct |
130 ms |
14532 KB |
Output is correct |
21 |
Correct |
58 ms |
7516 KB |
Output is correct |
22 |
Correct |
205 ms |
14544 KB |
Output is correct |
23 |
Correct |
188 ms |
12500 KB |
Output is correct |
24 |
Correct |
226 ms |
13580 KB |
Output is correct |
25 |
Correct |
188 ms |
14480 KB |
Output is correct |
26 |
Correct |
202 ms |
14536 KB |
Output is correct |
27 |
Correct |
108 ms |
14296 KB |
Output is correct |
28 |
Correct |
135 ms |
15060 KB |
Output is correct |
29 |
Correct |
187 ms |
15772 KB |
Output is correct |
30 |
Correct |
124 ms |
14776 KB |
Output is correct |
31 |
Correct |
167 ms |
14536 KB |
Output is correct |
32 |
Correct |
199 ms |
17104 KB |
Output is correct |
33 |
Correct |
142 ms |
14552 KB |
Output is correct |
34 |
Correct |
202 ms |
14508 KB |
Output is correct |
35 |
Correct |
106 ms |
14544 KB |
Output is correct |