# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
607327 |
2022-07-26T15:22:44 Z |
beaboss |
Mecho (IOI09_mecho) |
C++14 |
|
454 ms |
21404 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) {
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;
} 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 |
0 ms |
212 KB |
Output is correct |
6 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
7 |
Incorrect |
269 ms |
21240 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 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 |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
16 ms |
4308 KB |
Output is correct |
34 |
Correct |
15 ms |
4308 KB |
Output is correct |
35 |
Correct |
51 ms |
4308 KB |
Output is correct |
36 |
Correct |
20 ms |
5524 KB |
Output is correct |
37 |
Correct |
21 ms |
5524 KB |
Output is correct |
38 |
Correct |
74 ms |
5528 KB |
Output is correct |
39 |
Correct |
26 ms |
6932 KB |
Output is correct |
40 |
Correct |
28 ms |
6932 KB |
Output is correct |
41 |
Correct |
98 ms |
6924 KB |
Output is correct |
42 |
Correct |
30 ms |
8476 KB |
Output is correct |
43 |
Correct |
31 ms |
8496 KB |
Output is correct |
44 |
Correct |
116 ms |
8468 KB |
Output is correct |
45 |
Correct |
38 ms |
10180 KB |
Output is correct |
46 |
Correct |
39 ms |
10192 KB |
Output is correct |
47 |
Correct |
160 ms |
10172 KB |
Output is correct |
48 |
Correct |
48 ms |
12056 KB |
Output is correct |
49 |
Correct |
45 ms |
12060 KB |
Output is correct |
50 |
Correct |
185 ms |
12132 KB |
Output is correct |
51 |
Correct |
55 ms |
14044 KB |
Output is correct |
52 |
Correct |
56 ms |
14152 KB |
Output is correct |
53 |
Correct |
254 ms |
14272 KB |
Output is correct |
54 |
Correct |
66 ms |
16352 KB |
Output is correct |
55 |
Correct |
72 ms |
16356 KB |
Output is correct |
56 |
Correct |
264 ms |
16248 KB |
Output is correct |
57 |
Correct |
75 ms |
18676 KB |
Output is correct |
58 |
Correct |
82 ms |
18672 KB |
Output is correct |
59 |
Correct |
325 ms |
18592 KB |
Output is correct |
60 |
Correct |
95 ms |
21192 KB |
Output is correct |
61 |
Correct |
94 ms |
21192 KB |
Output is correct |
62 |
Correct |
454 ms |
21312 KB |
Output is correct |
63 |
Correct |
170 ms |
21168 KB |
Output is correct |
64 |
Correct |
348 ms |
21220 KB |
Output is correct |
65 |
Correct |
226 ms |
21204 KB |
Output is correct |
66 |
Correct |
211 ms |
21168 KB |
Output is correct |
67 |
Incorrect |
224 ms |
21208 KB |
Output isn't correct |
68 |
Correct |
112 ms |
21288 KB |
Output is correct |
69 |
Correct |
104 ms |
21304 KB |
Output is correct |
70 |
Correct |
107 ms |
21164 KB |
Output is correct |
71 |
Correct |
99 ms |
21244 KB |
Output is correct |
72 |
Incorrect |
112 ms |
21224 KB |
Output isn't correct |
73 |
Incorrect |
178 ms |
21404 KB |
Output isn't correct |
74 |
Correct |
166 ms |
21256 KB |
Output is correct |
75 |
Correct |
193 ms |
21368 KB |
Output is correct |
76 |
Correct |
195 ms |
21296 KB |
Output is correct |
77 |
Correct |
177 ms |
21256 KB |
Output is correct |
78 |
Incorrect |
235 ms |
21312 KB |
Output isn't correct |
79 |
Correct |
161 ms |
21208 KB |
Output is correct |
80 |
Correct |
174 ms |
21228 KB |
Output is correct |
81 |
Correct |
185 ms |
21232 KB |
Output is correct |
82 |
Correct |
174 ms |
21232 KB |
Output is correct |
83 |
Correct |
284 ms |
21340 KB |
Output is correct |
84 |
Correct |
218 ms |
21248 KB |
Output is correct |
85 |
Correct |
227 ms |
21232 KB |
Output is correct |
86 |
Correct |
241 ms |
21220 KB |
Output is correct |
87 |
Correct |
256 ms |
21388 KB |
Output is correct |
88 |
Correct |
279 ms |
21244 KB |
Output is correct |
89 |
Correct |
301 ms |
21220 KB |
Output is correct |
90 |
Correct |
275 ms |
21240 KB |
Output is correct |
91 |
Correct |
297 ms |
21256 KB |
Output is correct |
92 |
Correct |
301 ms |
21384 KB |
Output is correct |