# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
581490 | Saeed_247 | Mecho (IOI09_mecho) | C++14 | 255 ms | 12372 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}};
const int N = 800;
bool visited[N][N];
long long n, s;
vector<vector<char>> arr;
vector<pair<long long, long long>> hives;
pair<long long, long long> m, d;
queue<pair<long long, long long>> q;
bool move(int x, int y) {
return x >= 0 && y >= 0 && x < n && y < n;
}
bool check(long long tim, vector<vector<long long>>& dist, vector<vector<long long>>& swarm) {
memset(visited, false, sizeof visited);
while (q.size()) q.pop();
q.push(m);
visited[m.first][m.second] = true;
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') && !visited[x + xx][y + yy] && (dist[x + xx][y + yy] + tim < swarm[x + xx][y + yy] ||
(dist[x + xx][y + yy] + tim == swarm[x + xx][y + yy] && dist[d.first][d.second] == dist[x + xx][y + yy]))) {
q.push({x + xx, y + yy});
visited[x + xx][y + yy] = true;
}
}
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};
}
}
}
vector<vector<long long>> dist(n, vector<long long>(n, 1e18)), swarm(n, vector<long long>(n, 1e18));
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);
}
}
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});
}
}
long long l = 0, h = 1e9;
while (h - l > 1) {
long long mid = (h + l) / 2;
if (check(mid, dist, swarm)) l = mid;
else h = mid;
}
cout << l << '\n';
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |