# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
581481 | Saeed_247 | Mecho (IOI09_mecho) | C++17 | 1043 ms | 12064 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
template<typename T> void dout(string name, T arg) {
cerr << name << " = " << arg << "\e[39m" << endl;
}
template<typename T1, typename... T2> void dout(string names, T1 arg, T2... args) {
cerr << names.substr(0, names.find(',')) << " = " << arg << " | ";
dout(names.substr(names.find(',') + 2), args...);
}
#define debug(...) cerr << "\e[91m" << "[" << __LINE__ << ":" << __func__ << "]=> ", dout(#__VA_ARGS__, __VA_ARGS__);
const pair<int, int> dir[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
long long n, s;
vector<vector<char>> arr;
vector<pair<int, int>> hives;
pair<int, int> m, d;
queue<pair<int, int>> q;
bool move(int x, int y) {
return x >= 0 && y >= 0 && x < n && y < n;
}
bool check(long long tim) {
while (q.size()) q.pop();
vector<vector<long long>> dist(n, vector<long long>(n, INT_MAX)), swarm(n, vector<long long>(n, INT_MAX));
dist[m.first][m.second] = 0;
q.push(m);
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (auto [xx, yy] : dir) if (move(x + xx, y + yy) && (arr[x + xx][y + yy] == 'G' ||
arr[x + xx][y + yy] == 'D') && dist[x + xx][y + yy] > dist[x][y] + 1) {
dist[x + xx][y + yy] = dist[x][y] + 1;
q.push({x + xx, y + yy});
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dist[i][j] = dist[i][j] / s + (dist[i][j] % s != 0);
dist[i][j] += tim;
}
}
for (auto [x, y] : hives) {
swarm[x][y] = 0;
q.push({x, y});
}
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (auto [xx, yy] : dir) if (move(x + xx, y + yy) && arr[x + xx][y + yy] == 'G' && swarm[x + xx][y + yy] > swarm[x][y] + 1) {
swarm[x + xx][y + yy] = swarm[x][y] + 1;
q.push({x + xx, y + yy});
}
}
q.push(m);
dist[m.first][m.second] = -1;
while (!q.empty()) {
auto [x, y] = q.front();
if (q.front() == d) return true;
q.pop();
for (auto [xx, yy] : dir) if (move(x + xx, y + yy) && (arr[x + xx][y + yy] == 'G' ||
arr[x + xx][y + yy] == 'D') && dist[x + xx][y + yy] != -1 && (dist[x + xx][y + yy] < swarm[x + xx][y + yy] ||
(dist[x + xx][y + yy] == swarm[x + xx][y + yy] && dist[d.first][d.second] == dist[x + xx][y + yy]))) {
q.push({x + xx, y + yy});
dist[x + xx][y + yy] = -1;
}
}
return false;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> s;
arr.resize(n, vector<char> (n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
if (arr[i][j] == 'M') {
m = {i, j};
}
if (arr[i][j] == 'H') {
hives.push_back({i, j});
}
if (arr[i][j] == 'D') {
d = {i, j};
}
}
}
long long l = 0, h = 640000;
while (h - l > 1) {
int mid = (h + l) / 2;
if (check(mid)) l = mid;
else h = mid;
}
cout << l << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |