#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using indexedset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, s;
cin >> n >> s;
vector<vector<char>> grid(n, vector<char>(n));
queue<pair<ll, pair<int, int>>> h;
pair<int, int> m;
pair<int, int> dest;
vector<pair<int, int>> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<vector<ll>> dist(n, vector<ll>(n, 1e9));
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cin >> grid[i][j];
if (grid[i][j] == 'H'){
h.push({0, {i, j}});
dist[i][j] = 0;
}
if (grid[i][j] == 'M'){
m = {i, j};
}
if (grid[i][j] == 'D'){
dest = {i, j};
}
}
}
while (!h.empty()){
int x = h.front().second.first;
int y = h.front().second.second;
ll d = h.front().first;
h.pop();
if (dist[x][y] != d) continue;
for (auto ne : dir){
int nx = x + ne.first;
int ny = y + ne.second;
if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
if ((grid[nx][ny] == 'G' || grid[nx][ny] == 'M') && dist[nx][ny] > (d+1)){
h.push({d+1, {nx, ny}});
dist[nx][ny] = d+1;
}
}
}
function<bool(int)> works = [&](ll t){
queue<pair<ll, pair<int, int>>> q;
vector<vector<ll>> di(n, vector<ll>(n, 1e9));
di[m.first][m.second] = 0;
q.push({0, m});
while (!q.empty()){
int x = q.front().second.first;
int y = q.front().second.second;
ll d = q.front().first;
q.pop();
if (di[x][y] != d) continue;
for (auto ne : dir){
int nx = x + ne.first;
int ny = y + ne.second;
if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
if ((grid[nx][ny] == 'G' || grid[nx][ny] == 'D') && di[nx][ny] > (d+1) && (d+1) < s*(-t+dist[nx][ny])){
q.push({d+1, {nx, ny}});
di[nx][ny] = d+1;
}
}
}
if (di[dest.first][dest.second] == 1e9){
return false;
}
return true;
};
ll l = -1, r = n*n+1;
while (l < r){
ll m = (l+r+1)/2;
if (works(m)){
l = m;
}else{
r = m-1;
}
}
cout << l << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
93 ms |
11320 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
7 ms |
2528 KB |
Output is correct |
34 |
Correct |
7 ms |
2528 KB |
Output is correct |
35 |
Correct |
20 ms |
2552 KB |
Output is correct |
36 |
Correct |
12 ms |
3164 KB |
Output is correct |
37 |
Correct |
11 ms |
3184 KB |
Output is correct |
38 |
Correct |
28 ms |
3176 KB |
Output is correct |
39 |
Correct |
13 ms |
3932 KB |
Output is correct |
40 |
Correct |
13 ms |
3896 KB |
Output is correct |
41 |
Correct |
38 ms |
3940 KB |
Output is correct |
42 |
Correct |
17 ms |
4708 KB |
Output is correct |
43 |
Correct |
16 ms |
4672 KB |
Output is correct |
44 |
Correct |
46 ms |
4704 KB |
Output is correct |
45 |
Correct |
19 ms |
5612 KB |
Output is correct |
46 |
Correct |
20 ms |
5612 KB |
Output is correct |
47 |
Correct |
56 ms |
5564 KB |
Output is correct |
48 |
Correct |
24 ms |
6556 KB |
Output is correct |
49 |
Correct |
25 ms |
6620 KB |
Output is correct |
50 |
Correct |
64 ms |
6560 KB |
Output is correct |
51 |
Correct |
29 ms |
7632 KB |
Output is correct |
52 |
Correct |
33 ms |
7568 KB |
Output is correct |
53 |
Correct |
78 ms |
7616 KB |
Output is correct |
54 |
Correct |
32 ms |
8772 KB |
Output is correct |
55 |
Correct |
32 ms |
8724 KB |
Output is correct |
56 |
Correct |
90 ms |
8756 KB |
Output is correct |
57 |
Correct |
46 ms |
9960 KB |
Output is correct |
58 |
Correct |
42 ms |
9984 KB |
Output is correct |
59 |
Correct |
104 ms |
9936 KB |
Output is correct |
60 |
Correct |
43 ms |
11332 KB |
Output is correct |
61 |
Correct |
43 ms |
11324 KB |
Output is correct |
62 |
Correct |
122 ms |
11324 KB |
Output is correct |
63 |
Correct |
111 ms |
11316 KB |
Output is correct |
64 |
Correct |
198 ms |
11224 KB |
Output is correct |
65 |
Correct |
152 ms |
11268 KB |
Output is correct |
66 |
Correct |
125 ms |
11260 KB |
Output is correct |
67 |
Correct |
126 ms |
11208 KB |
Output is correct |
68 |
Correct |
80 ms |
11312 KB |
Output is correct |
69 |
Correct |
66 ms |
11260 KB |
Output is correct |
70 |
Correct |
56 ms |
11248 KB |
Output is correct |
71 |
Correct |
62 ms |
11384 KB |
Output is correct |
72 |
Incorrect |
51 ms |
11248 KB |
Output isn't correct |
73 |
Incorrect |
67 ms |
11340 KB |
Output isn't correct |
74 |
Correct |
74 ms |
11220 KB |
Output is correct |
75 |
Correct |
85 ms |
11300 KB |
Output is correct |
76 |
Correct |
74 ms |
11304 KB |
Output is correct |
77 |
Correct |
73 ms |
11324 KB |
Output is correct |
78 |
Correct |
88 ms |
11304 KB |
Output is correct |
79 |
Correct |
63 ms |
11284 KB |
Output is correct |
80 |
Correct |
71 ms |
11280 KB |
Output is correct |
81 |
Correct |
81 ms |
11364 KB |
Output is correct |
82 |
Correct |
80 ms |
11252 KB |
Output is correct |
83 |
Correct |
86 ms |
11304 KB |
Output is correct |
84 |
Correct |
82 ms |
11284 KB |
Output is correct |
85 |
Correct |
94 ms |
11292 KB |
Output is correct |
86 |
Correct |
86 ms |
11280 KB |
Output is correct |
87 |
Correct |
89 ms |
11408 KB |
Output is correct |
88 |
Correct |
90 ms |
11344 KB |
Output is correct |
89 |
Correct |
87 ms |
11300 KB |
Output is correct |
90 |
Correct |
95 ms |
11284 KB |
Output is correct |
91 |
Correct |
96 ms |
11324 KB |
Output is correct |
92 |
Correct |
100 ms |
11276 KB |
Output is correct |