#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));
if ((dist[m.first][m.second]-t) <= 0){
return false;
}
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;
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 |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
100 ms |
11324 KB |
Output is correct |
8 |
Correct |
0 ms |
348 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 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is 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 |
0 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 |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 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 |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
8 ms |
2528 KB |
Output is correct |
34 |
Correct |
8 ms |
2528 KB |
Output is correct |
35 |
Correct |
22 ms |
2580 KB |
Output is correct |
36 |
Correct |
10 ms |
3164 KB |
Output is correct |
37 |
Correct |
11 ms |
3188 KB |
Output is correct |
38 |
Correct |
27 ms |
3216 KB |
Output is correct |
39 |
Correct |
13 ms |
3928 KB |
Output is correct |
40 |
Correct |
13 ms |
3868 KB |
Output is correct |
41 |
Correct |
37 ms |
3900 KB |
Output is correct |
42 |
Correct |
20 ms |
4708 KB |
Output is correct |
43 |
Correct |
17 ms |
4704 KB |
Output is correct |
44 |
Correct |
45 ms |
4708 KB |
Output is correct |
45 |
Correct |
20 ms |
5560 KB |
Output is correct |
46 |
Correct |
20 ms |
5572 KB |
Output is correct |
47 |
Correct |
51 ms |
5564 KB |
Output is correct |
48 |
Correct |
24 ms |
6568 KB |
Output is correct |
49 |
Correct |
30 ms |
6508 KB |
Output is correct |
50 |
Correct |
65 ms |
6552 KB |
Output is correct |
51 |
Correct |
27 ms |
7516 KB |
Output is correct |
52 |
Correct |
29 ms |
7592 KB |
Output is correct |
53 |
Correct |
80 ms |
7652 KB |
Output is correct |
54 |
Correct |
34 ms |
8760 KB |
Output is correct |
55 |
Correct |
34 ms |
8764 KB |
Output is correct |
56 |
Correct |
101 ms |
8752 KB |
Output is correct |
57 |
Correct |
41 ms |
9972 KB |
Output is correct |
58 |
Correct |
38 ms |
9956 KB |
Output is correct |
59 |
Correct |
107 ms |
9968 KB |
Output is correct |
60 |
Correct |
57 ms |
11260 KB |
Output is correct |
61 |
Correct |
48 ms |
11320 KB |
Output is correct |
62 |
Correct |
121 ms |
11324 KB |
Output is correct |
63 |
Correct |
109 ms |
11284 KB |
Output is correct |
64 |
Correct |
191 ms |
11208 KB |
Output is correct |
65 |
Correct |
165 ms |
11220 KB |
Output is correct |
66 |
Correct |
125 ms |
11248 KB |
Output is correct |
67 |
Correct |
123 ms |
11252 KB |
Output is correct |
68 |
Correct |
65 ms |
11292 KB |
Output is correct |
69 |
Correct |
74 ms |
11260 KB |
Output is correct |
70 |
Correct |
64 ms |
11248 KB |
Output is correct |
71 |
Correct |
63 ms |
11304 KB |
Output is correct |
72 |
Correct |
53 ms |
11256 KB |
Output is correct |
73 |
Correct |
54 ms |
11392 KB |
Output is correct |
74 |
Correct |
91 ms |
11344 KB |
Output is correct |
75 |
Correct |
86 ms |
11296 KB |
Output is correct |
76 |
Correct |
80 ms |
11320 KB |
Output is correct |
77 |
Correct |
85 ms |
11360 KB |
Output is correct |
78 |
Correct |
87 ms |
11288 KB |
Output is correct |
79 |
Correct |
70 ms |
11252 KB |
Output is correct |
80 |
Correct |
73 ms |
11308 KB |
Output is correct |
81 |
Correct |
78 ms |
11356 KB |
Output is correct |
82 |
Correct |
77 ms |
11332 KB |
Output is correct |
83 |
Correct |
85 ms |
11300 KB |
Output is correct |
84 |
Correct |
81 ms |
11280 KB |
Output is correct |
85 |
Correct |
85 ms |
11288 KB |
Output is correct |
86 |
Correct |
85 ms |
11336 KB |
Output is correct |
87 |
Correct |
83 ms |
11320 KB |
Output is correct |
88 |
Correct |
90 ms |
11272 KB |
Output is correct |
89 |
Correct |
110 ms |
11296 KB |
Output is correct |
90 |
Correct |
90 ms |
11284 KB |
Output is correct |
91 |
Correct |
129 ms |
11296 KB |
Output is correct |
92 |
Correct |
135 ms |
11280 KB |
Output is correct |