#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 = 0, 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 |
1 ms |
348 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 |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
92 ms |
11332 KB |
Output is correct |
8 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 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 |
1 ms |
348 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
348 KB |
Output isn't 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 |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
452 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 |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
8 ms |
2612 KB |
Output is correct |
34 |
Correct |
9 ms |
2624 KB |
Output is correct |
35 |
Correct |
20 ms |
2700 KB |
Output is correct |
36 |
Correct |
11 ms |
3292 KB |
Output is correct |
37 |
Correct |
10 ms |
3276 KB |
Output is correct |
38 |
Correct |
27 ms |
3308 KB |
Output is correct |
39 |
Correct |
18 ms |
4060 KB |
Output is correct |
40 |
Correct |
14 ms |
4060 KB |
Output is correct |
41 |
Correct |
42 ms |
4052 KB |
Output is correct |
42 |
Correct |
16 ms |
4884 KB |
Output is correct |
43 |
Correct |
18 ms |
4884 KB |
Output is correct |
44 |
Correct |
46 ms |
4880 KB |
Output is correct |
45 |
Correct |
20 ms |
5780 KB |
Output is correct |
46 |
Correct |
22 ms |
5784 KB |
Output is correct |
47 |
Correct |
54 ms |
5864 KB |
Output is correct |
48 |
Correct |
24 ms |
6772 KB |
Output is correct |
49 |
Correct |
25 ms |
6756 KB |
Output is correct |
50 |
Correct |
70 ms |
6796 KB |
Output is correct |
51 |
Correct |
29 ms |
7888 KB |
Output is correct |
52 |
Correct |
30 ms |
7852 KB |
Output is correct |
53 |
Correct |
74 ms |
7896 KB |
Output is correct |
54 |
Correct |
34 ms |
9340 KB |
Output is correct |
55 |
Correct |
44 ms |
9100 KB |
Output is correct |
56 |
Correct |
96 ms |
9004 KB |
Output is correct |
57 |
Correct |
37 ms |
10284 KB |
Output is correct |
58 |
Correct |
39 ms |
10356 KB |
Output is correct |
59 |
Correct |
106 ms |
10352 KB |
Output is correct |
60 |
Correct |
50 ms |
11712 KB |
Output is correct |
61 |
Correct |
45 ms |
11692 KB |
Output is correct |
62 |
Correct |
122 ms |
11764 KB |
Output is correct |
63 |
Correct |
104 ms |
11612 KB |
Output is correct |
64 |
Correct |
183 ms |
11596 KB |
Output is correct |
65 |
Correct |
152 ms |
11680 KB |
Output is correct |
66 |
Correct |
125 ms |
11620 KB |
Output is correct |
67 |
Incorrect |
130 ms |
11664 KB |
Output isn't correct |
68 |
Correct |
66 ms |
11716 KB |
Output is correct |
69 |
Correct |
62 ms |
11692 KB |
Output is correct |
70 |
Correct |
64 ms |
11736 KB |
Output is correct |
71 |
Correct |
58 ms |
11748 KB |
Output is correct |
72 |
Incorrect |
53 ms |
11776 KB |
Output isn't correct |
73 |
Incorrect |
57 ms |
11792 KB |
Output isn't correct |
74 |
Correct |
74 ms |
11720 KB |
Output is correct |
75 |
Correct |
82 ms |
11728 KB |
Output is correct |
76 |
Correct |
80 ms |
11700 KB |
Output is correct |
77 |
Correct |
91 ms |
11788 KB |
Output is correct |
78 |
Incorrect |
91 ms |
11948 KB |
Output isn't correct |
79 |
Correct |
66 ms |
11760 KB |
Output is correct |
80 |
Correct |
71 ms |
11704 KB |
Output is correct |
81 |
Correct |
76 ms |
11632 KB |
Output is correct |
82 |
Correct |
72 ms |
11680 KB |
Output is correct |
83 |
Correct |
88 ms |
11692 KB |
Output is correct |
84 |
Correct |
85 ms |
11580 KB |
Output is correct |
85 |
Correct |
90 ms |
11704 KB |
Output is correct |
86 |
Correct |
88 ms |
11704 KB |
Output is correct |
87 |
Correct |
86 ms |
11752 KB |
Output is correct |
88 |
Correct |
94 ms |
11744 KB |
Output is correct |
89 |
Correct |
97 ms |
11660 KB |
Output is correct |
90 |
Correct |
101 ms |
11728 KB |
Output is correct |
91 |
Correct |
90 ms |
11688 KB |
Output is correct |
92 |
Correct |
93 ms |
11716 KB |
Output is correct |