#pragma GCC optimize("-O3")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define vl vector<ll>
#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define p_b pop_back
#define f first
#define s second
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const ll sz = 850;
char grid[sz][sz];
ll dist[sz][sz], ch[sz][sz], n, s, i, j, k;
ll dx[] = {0, 1, 0, -1};
ll dy[] = {1, 0, -1, 0};
pll st, en;
ll bfs(ll mid)
{
if(dist[st.f][st.s] - mid * s <= 0)
return 0;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
ch[i][j] = 0;
}
queue<pll>q;
q.push(st);
while(!q.empty())
{
pll node = q.front();
q.pop();
for(k = 0; k < 4; k++)
{
pll nnode = {node.f + dx[k], node.s + dy[k]};
if(nnode.f >= 1 && nnode.f <= n && nnode.s >= 1 && nnode.s <= n)
{
if(ch[nnode.f][nnode.s] == 0 && grid[nnode.f][nnode.s] != 'T' && grid[nnode.f][nnode.s] != 'M' && dist[nnode.f][nnode.s] - mid * s > ch[node.f][node.s] + 1)
{
ch[nnode.f][nnode.s] = ch[node.f][node.s] + 1;
q.push(nnode);
}
}
}
}
if(ch[en.f][en.s] > 0)
return 1;
return 0;
}
void solve()
{
cin >> n >> s;
queue<pll>q;
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++){
cin >> grid[i][j];
if(grid[i][j] == 'M')
st = {i, j};
else if(grid[i][j] == 'D')
en = {i, j};
else if(grid[i][j] == 'H')
q.push({i, j});
}
}
while(!q.empty())
{
pll node = q.front();
q.pop();
for(k = 0; k < 4; k++)
{
pll nnode = {node.f + dx[k], node.s + dy[k]};
if(nnode.f >= 1 && nnode.f <= n && nnode.s >= 1 && nnode.s <= n)
{
if(dist[nnode.f][nnode.s] == 0 && grid[nnode.f][nnode.s] != 'H' && grid[nnode.f][nnode.s] != 'T' && grid[nnode.f][nnode.s] != 'D')
{
dist[nnode.f][nnode.s] = dist[node.f][node.s] + s;
q.push(nnode);
}
}
}
}
dist[en.f][en.s] = LLONG_MAX;
ll l = 0, r = 1000000000, ans = -1;
while(l <= r)
{
ll mid = (l + r) / 2;
if(bfs(mid))
{
ans = max(ans, mid);
l = mid + 1;
}
else
r = mid - 1;
}
cout << ans << "\n";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll tests = 1;
//cin >> tests;
while(tests--)
{
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
464 KB |
Output is correct |
2 |
Correct |
1 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
59 ms |
11856 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2764 KB |
Output is correct |
14 |
Correct |
1 ms |
2904 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2768 KB |
Output is correct |
24 |
Correct |
1 ms |
2908 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
1 ms |
2908 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
1 ms |
2772 KB |
Output is correct |
30 |
Correct |
1 ms |
2908 KB |
Output is correct |
31 |
Correct |
1 ms |
2904 KB |
Output is correct |
32 |
Correct |
1 ms |
3104 KB |
Output is correct |
33 |
Correct |
5 ms |
6984 KB |
Output is correct |
34 |
Correct |
5 ms |
7004 KB |
Output is correct |
35 |
Correct |
13 ms |
7256 KB |
Output is correct |
36 |
Correct |
7 ms |
7260 KB |
Output is correct |
37 |
Correct |
8 ms |
7516 KB |
Output is correct |
38 |
Correct |
17 ms |
7768 KB |
Output is correct |
39 |
Correct |
6 ms |
7516 KB |
Output is correct |
40 |
Correct |
6 ms |
8028 KB |
Output is correct |
41 |
Correct |
26 ms |
8284 KB |
Output is correct |
42 |
Correct |
7 ms |
8028 KB |
Output is correct |
43 |
Correct |
6 ms |
8284 KB |
Output is correct |
44 |
Correct |
31 ms |
8540 KB |
Output is correct |
45 |
Correct |
8 ms |
8280 KB |
Output is correct |
46 |
Correct |
8 ms |
8784 KB |
Output is correct |
47 |
Correct |
35 ms |
9096 KB |
Output is correct |
48 |
Correct |
9 ms |
8540 KB |
Output is correct |
49 |
Correct |
10 ms |
9044 KB |
Output is correct |
50 |
Correct |
39 ms |
9308 KB |
Output is correct |
51 |
Correct |
13 ms |
8976 KB |
Output is correct |
52 |
Correct |
12 ms |
9560 KB |
Output is correct |
53 |
Correct |
55 ms |
9964 KB |
Output is correct |
54 |
Correct |
14 ms |
9564 KB |
Output is correct |
55 |
Correct |
15 ms |
10308 KB |
Output is correct |
56 |
Correct |
54 ms |
10672 KB |
Output is correct |
57 |
Correct |
13 ms |
10328 KB |
Output is correct |
58 |
Correct |
16 ms |
10836 KB |
Output is correct |
59 |
Correct |
72 ms |
11428 KB |
Output is correct |
60 |
Correct |
19 ms |
11100 KB |
Output is correct |
61 |
Correct |
17 ms |
11704 KB |
Output is correct |
62 |
Correct |
120 ms |
12116 KB |
Output is correct |
63 |
Correct |
84 ms |
12140 KB |
Output is correct |
64 |
Correct |
140 ms |
12112 KB |
Output is correct |
65 |
Correct |
131 ms |
12112 KB |
Output is correct |
66 |
Correct |
85 ms |
12108 KB |
Output is correct |
67 |
Correct |
80 ms |
12180 KB |
Output is correct |
68 |
Correct |
38 ms |
12256 KB |
Output is correct |
69 |
Correct |
36 ms |
12112 KB |
Output is correct |
70 |
Correct |
27 ms |
12112 KB |
Output is correct |
71 |
Correct |
30 ms |
12288 KB |
Output is correct |
72 |
Correct |
26 ms |
12052 KB |
Output is correct |
73 |
Correct |
29 ms |
12684 KB |
Output is correct |
74 |
Correct |
39 ms |
12628 KB |
Output is correct |
75 |
Correct |
45 ms |
12584 KB |
Output is correct |
76 |
Correct |
39 ms |
12636 KB |
Output is correct |
77 |
Correct |
44 ms |
12636 KB |
Output is correct |
78 |
Correct |
41 ms |
12592 KB |
Output is correct |
79 |
Correct |
37 ms |
12500 KB |
Output is correct |
80 |
Correct |
40 ms |
12636 KB |
Output is correct |
81 |
Correct |
41 ms |
12616 KB |
Output is correct |
82 |
Correct |
38 ms |
12628 KB |
Output is correct |
83 |
Correct |
48 ms |
12368 KB |
Output is correct |
84 |
Correct |
42 ms |
12380 KB |
Output is correct |
85 |
Correct |
43 ms |
12380 KB |
Output is correct |
86 |
Correct |
57 ms |
12628 KB |
Output is correct |
87 |
Correct |
44 ms |
12372 KB |
Output is correct |
88 |
Correct |
51 ms |
12368 KB |
Output is correct |
89 |
Correct |
63 ms |
12464 KB |
Output is correct |
90 |
Correct |
52 ms |
12528 KB |
Output is correct |
91 |
Correct |
48 ms |
12368 KB |
Output is correct |
92 |
Correct |
50 ms |
12284 KB |
Output is correct |