# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
607336 |
2022-07-26T15:27:10 Z |
beaboss |
Mecho (IOI09_mecho) |
C++14 |
|
370 ms |
21384 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, s;
cin >> n>> s;
vector<vector<char> > grid(n, vector<char>(n));
queue<pair<ll ,ll> > q1;
queue<pair<ll ,ll> > q2;
pair<ll ,ll> home;
pair<ll ,ll> start;
vector<vector<ll> > hdist(n, vector<ll>(n, -1));
for (ll i = 0; i < n; i++) for (ll j = 0; j < n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'H') {
q1.push({i, j});
hdist[i][j] = 0;
}
else if (grid[i][j] == 'M') {
// q2.push({i, j});
start = {i, j};
}
else if (grid[i][j] == 'D') home = {i, j};
}
vector<ll> dx = {1, -1, 0, 0};
vector<ll> dy = {0, 0, -1, 1};
while (!q1.empty() && hdist[home.first][home.second] == -1) {
pair<ll, ll> curr = q1.front();
q1.pop();
for (ll i = 0; i < 4; i++) {
ll x = curr.first + dx[i];
ll y = curr.second + dy[i];
if (x >= 0 && x < n && y >= 0 && y < n) {
if (hdist[x][y] == -1 && (grid[x][y] == 'G' || grid[x][y] == 'D')) {
hdist[x][y] = hdist[curr.first][curr.second] + s;
q1.push({x, y});
}
}
}
}
vector<vector<pair<ll, ll> > > parent(n, vector<pair<ll, ll> >(n, {-1, -1}));
int hi = n*n;
int lo = 0;
while (hi > lo || (hi == 0 && lo == 0)) {
int mid = (hi + lo + 1)/2;
// cout << hi << lo << endl;
vector<vector<ll> > mdist(n, vector<ll>(n, -1));
mdist[start.first][start.second] = mid * s;
q2.push(start);
// cout << hdist[3][1] << endl;
while (!q2.empty()) {
pair<ll, ll> curr = q2.front();
// cout << mdist[curr.first][curr.second] << endl;
q2.pop();
for (ll i = 0; i < 4; i++) {
ll x = curr.first + dx[i];
ll y = curr.second + dy[i];
if (x >= 0 && x < n && y >= 0 && y < n) {
if (mdist[x][y] == -1 && (grid[x][y] == 'G' || grid[x][y] == 'D') && (hdist[x][y] > mdist[curr.first][curr.second] + 1 || hdist[x][y ] == -1)) {
mdist[x][y] = mdist[curr.first][curr.second] + 1;
q2.push({x, y});
parent[x][y] = {curr.first, curr.second};
}
}
}
}
if (mdist[home.first][home.second] != -1) {
lo = mid;
if (hi == 0 && lo == 0) break;
} else {
hi = mid - 1;
}
}
// ll ans = 100000000;
// pair<ll ,ll> current = home;
// // cout << mdist[home.first][home.second] << endl;
// if (mdist[home.first][home.second] == -1) {
// cout << -1 << endl;
// return 0;
// }
// while (current != start) {
// ll x = current.first;
// ll y = current.second;
// if (hdist[x][y] != -1) {
// // cout << x << y << hdist[x][y] << endl;
// ans = min(ans, hdist[x][y]-mdist[x][y]);
// }
// current = parent[x][y];
// }
// for (ll i = 0; i < 4; i++) {
// ll x = home.first + dx[i];
// ll y = home.second + dy[i];
// if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] != 'T') {
// ans = max(hdist[x][y] - mdist[x][y], ans);
// cout << mdist[x][y] << hdist[x][y] << x << y << endl;
// }
// }
cout << hi << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Incorrect |
298 ms |
21264 KB |
Output isn't correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
480 KB |
Output is correct |
33 |
Correct |
15 ms |
4308 KB |
Output is correct |
34 |
Correct |
15 ms |
4308 KB |
Output is correct |
35 |
Correct |
48 ms |
4316 KB |
Output is correct |
36 |
Correct |
20 ms |
5588 KB |
Output is correct |
37 |
Correct |
21 ms |
5604 KB |
Output is correct |
38 |
Correct |
70 ms |
5536 KB |
Output is correct |
39 |
Correct |
24 ms |
6924 KB |
Output is correct |
40 |
Correct |
26 ms |
6868 KB |
Output is correct |
41 |
Correct |
86 ms |
6892 KB |
Output is correct |
42 |
Correct |
30 ms |
8500 KB |
Output is correct |
43 |
Correct |
31 ms |
8500 KB |
Output is correct |
44 |
Correct |
119 ms |
8588 KB |
Output is correct |
45 |
Correct |
39 ms |
10188 KB |
Output is correct |
46 |
Correct |
45 ms |
10240 KB |
Output is correct |
47 |
Correct |
151 ms |
10172 KB |
Output is correct |
48 |
Correct |
43 ms |
12104 KB |
Output is correct |
49 |
Correct |
45 ms |
12052 KB |
Output is correct |
50 |
Correct |
189 ms |
12124 KB |
Output is correct |
51 |
Correct |
54 ms |
14060 KB |
Output is correct |
52 |
Correct |
56 ms |
14128 KB |
Output is correct |
53 |
Correct |
220 ms |
14068 KB |
Output is correct |
54 |
Correct |
65 ms |
16356 KB |
Output is correct |
55 |
Correct |
65 ms |
16364 KB |
Output is correct |
56 |
Correct |
279 ms |
16248 KB |
Output is correct |
57 |
Correct |
77 ms |
18676 KB |
Output is correct |
58 |
Correct |
83 ms |
18716 KB |
Output is correct |
59 |
Correct |
324 ms |
18588 KB |
Output is correct |
60 |
Correct |
84 ms |
21252 KB |
Output is correct |
61 |
Correct |
82 ms |
21192 KB |
Output is correct |
62 |
Correct |
370 ms |
21148 KB |
Output is correct |
63 |
Correct |
204 ms |
21184 KB |
Output is correct |
64 |
Correct |
287 ms |
21112 KB |
Output is correct |
65 |
Correct |
235 ms |
21136 KB |
Output is correct |
66 |
Correct |
196 ms |
21160 KB |
Output is correct |
67 |
Correct |
205 ms |
21224 KB |
Output is correct |
68 |
Correct |
122 ms |
21264 KB |
Output is correct |
69 |
Correct |
104 ms |
21188 KB |
Output is correct |
70 |
Correct |
105 ms |
21200 KB |
Output is correct |
71 |
Correct |
103 ms |
21236 KB |
Output is correct |
72 |
Incorrect |
102 ms |
21220 KB |
Output isn't correct |
73 |
Incorrect |
211 ms |
21268 KB |
Output isn't correct |
74 |
Correct |
188 ms |
21308 KB |
Output is correct |
75 |
Correct |
180 ms |
21224 KB |
Output is correct |
76 |
Correct |
252 ms |
21204 KB |
Output is correct |
77 |
Correct |
182 ms |
21296 KB |
Output is correct |
78 |
Correct |
259 ms |
21244 KB |
Output is correct |
79 |
Correct |
170 ms |
21248 KB |
Output is correct |
80 |
Correct |
162 ms |
21252 KB |
Output is correct |
81 |
Correct |
178 ms |
21352 KB |
Output is correct |
82 |
Correct |
169 ms |
21232 KB |
Output is correct |
83 |
Correct |
245 ms |
21320 KB |
Output is correct |
84 |
Correct |
226 ms |
21248 KB |
Output is correct |
85 |
Correct |
239 ms |
21252 KB |
Output is correct |
86 |
Correct |
279 ms |
21332 KB |
Output is correct |
87 |
Correct |
251 ms |
21212 KB |
Output is correct |
88 |
Correct |
283 ms |
21344 KB |
Output is correct |
89 |
Correct |
284 ms |
21256 KB |
Output is correct |
90 |
Correct |
288 ms |
21208 KB |
Output is correct |
91 |
Correct |
263 ms |
21384 KB |
Output is correct |
92 |
Correct |
268 ms |
21224 KB |
Output is correct |