Submission #607333

# Submission time Handle Problem Language Result Execution time Memory
607333 2022-07-26T15:25:16 Z beaboss Mecho (IOI09_mecho) C++14
58 / 100
1000 ms 21408 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;
		} 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 1 ms 212 KB Output isn't correct
3 Correct 1 ms 212 KB Output is correct
4 Execution timed out 1087 ms 212 KB Time limit exceeded
5 Execution timed out 1098 ms 212 KB Time limit exceeded
6 Incorrect 1 ms 212 KB Output isn't correct
7 Incorrect 407 ms 21228 KB Output isn't correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 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 424 KB Output is 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 1 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 468 KB Output is correct
33 Correct 16 ms 4308 KB Output is correct
34 Correct 19 ms 4344 KB Output is correct
35 Correct 52 ms 4328 KB Output is correct
36 Correct 20 ms 5520 KB Output is correct
37 Correct 21 ms 5524 KB Output is correct
38 Correct 69 ms 5560 KB Output is correct
39 Correct 32 ms 6908 KB Output is correct
40 Correct 32 ms 6920 KB Output is correct
41 Correct 90 ms 6928 KB Output is correct
42 Correct 32 ms 8488 KB Output is correct
43 Correct 34 ms 8488 KB Output is correct
44 Correct 121 ms 8452 KB Output is correct
45 Correct 41 ms 10192 KB Output is correct
46 Correct 44 ms 10188 KB Output is correct
47 Correct 149 ms 10160 KB Output is correct
48 Correct 47 ms 12044 KB Output is correct
49 Correct 48 ms 12092 KB Output is correct
50 Correct 214 ms 12016 KB Output is correct
51 Correct 55 ms 14076 KB Output is correct
52 Correct 57 ms 14056 KB Output is correct
53 Correct 221 ms 14068 KB Output is correct
54 Correct 66 ms 16268 KB Output is correct
55 Correct 74 ms 16320 KB Output is correct
56 Correct 273 ms 16236 KB Output is correct
57 Correct 81 ms 18640 KB Output is correct
58 Correct 77 ms 18696 KB Output is correct
59 Correct 354 ms 18572 KB Output is correct
60 Correct 92 ms 21172 KB Output is correct
61 Correct 87 ms 21248 KB Output is correct
62 Correct 401 ms 21100 KB Output is correct
63 Execution timed out 1100 ms 21168 KB Time limit exceeded
64 Correct 289 ms 21164 KB Output is correct
65 Correct 249 ms 21136 KB Output is correct
66 Correct 205 ms 21180 KB Output is correct
67 Correct 217 ms 21148 KB Output is correct
68 Execution timed out 1097 ms 21276 KB Time limit exceeded
69 Correct 129 ms 21332 KB Output is correct
70 Correct 111 ms 21184 KB Output is correct
71 Correct 113 ms 21264 KB Output is correct
72 Incorrect 122 ms 21180 KB Output isn't correct
73 Incorrect 196 ms 21336 KB Output isn't correct
74 Correct 168 ms 21380 KB Output is correct
75 Correct 237 ms 21212 KB Output is correct
76 Correct 171 ms 21272 KB Output is correct
77 Correct 193 ms 21272 KB Output is correct
78 Correct 254 ms 21248 KB Output is correct
79 Correct 170 ms 21236 KB Output is correct
80 Correct 176 ms 21236 KB Output is correct
81 Correct 199 ms 21272 KB Output is correct
82 Correct 194 ms 21248 KB Output is correct
83 Execution timed out 1089 ms 21408 KB Time limit exceeded
84 Correct 240 ms 21228 KB Output is correct
85 Correct 277 ms 21220 KB Output is correct
86 Correct 240 ms 21216 KB Output is correct
87 Correct 236 ms 21224 KB Output is correct
88 Execution timed out 1086 ms 21236 KB Time limit exceeded
89 Correct 283 ms 21220 KB Output is correct
90 Correct 322 ms 21196 KB Output is correct
91 Correct 444 ms 21316 KB Output is correct
92 Correct 286 ms 21216 KB Output is correct