Submission #254317

# Submission time Handle Problem Language Result Execution time Memory
254317 2020-07-29T20:39:52 Z sandoval Mecho (IOI09_mecho) C++11
100 / 100
209 ms 8916 KB
#include <bits/stdc++.h>

using namespace std;
using ii = pair<int,int>;
using ll = long long;

const int dx[] {0, -1, 0, 1};
const int dy[] {-1, 0, 1, 0};

constexpr int MAXS = 5+800;
constexpr int INF = 1e8;
char c[MAXS][MAXS];
int N,S, tb[MAXS][MAXS];

bool ok(int x, int y) {
  return x >= 0 && y >= 0 && x < N && y < N;
}

void bfs_bees() {
  queue<ii> q;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (c[i][j] == 'H') {
        q.push({i,j});
        tb[i][j] = 0;
      } else {
        tb[i][j] = 10*INF;
      }
    }
  }

  while (!q.empty()) {
    auto u = q.front(); q.pop();
    for (int k = 0; k < 4; ++k) {
      int newx = u.first+dx[k], newy = u.second+dy[k];
      if (ok(newx, newy) && 1+tb[u.first][u.second] < tb[newx][newy] && (c[newx][newy] == 'G' || c[newx][newy] == 'M')) {
        q.push({newx, newy});
        tb[newx][newy] = 1+tb[u.first][u.second];
      }
    }
  }
}


ii dist[MAXS][MAXS];
bool can(const ii& source, const ii& target, int t) {
  if (t >= tb[source.first][source.second]) return false;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      dist[i][j] = {10*INF, 0};
    }
  }

  queue<array<int,2>> q; q.push({source.first, source.second});
  dist[source.first][source.second] = {t, S};

  while (!q.empty()) {
    auto u = q.front(); q.pop();
    if (u[0] == target.first && u[1] == target.second) return true;
    for (int k = 0; k < 4; ++k) {
      int newx = u[0]+dx[k], newy = u[1]+dy[k];
      ii newcost = dist[u[0]][u[1]];
      newcost.second--;

      if (newcost.second <= 0) {
        newcost.first++;
        newcost.second = S;
      }

      if (ok(newx, newy) && (c[newx][newy] == 'G' || c[newx][newy] == 'D' || c[newx][newy] == 'M') && newcost.first < tb[newx][newy] && newcost.first < dist[newx][newy].first) {
        q.push({newx, newy});
        dist[newx][newy] = newcost;
      }
    }
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  cin >> N >> S;
  ii start, target;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> c[i][j];
      if (c[i][j] == 'M') {
        start = {i,j};
      } else if (c[i][j] == 'D') {
        target = {i,j};
      }
    }
  }

  bfs_bees();

  int low = 0, high = INF, best = -1;
  while (low <= high) {
    int mid = (low+high) >> 1;
    if (can(start, target, mid)) {
      low = 1+mid;
      best = mid;
    } else {
      high = mid-1;
    }
  }
  cout << best << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 384 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 120 ms 8812 KB Output is correct
8 Correct 0 ms 512 KB Output is correct
9 Correct 0 ms 512 KB Output is correct
10 Correct 0 ms 512 KB Output is correct
11 Correct 0 ms 512 KB Output is correct
12 Correct 1 ms 896 KB Output is correct
13 Correct 1 ms 768 KB Output is correct
14 Correct 1 ms 896 KB Output is correct
15 Correct 0 ms 512 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 0 ms 512 KB Output is correct
18 Correct 1 ms 512 KB Output is correct
19 Correct 1 ms 640 KB Output is correct
20 Correct 1 ms 640 KB Output is correct
21 Correct 1 ms 640 KB Output is correct
22 Correct 1 ms 640 KB Output is correct
23 Correct 1 ms 768 KB Output is correct
24 Correct 1 ms 768 KB Output is correct
25 Correct 1 ms 896 KB Output is correct
26 Correct 1 ms 896 KB Output is correct
27 Correct 1 ms 896 KB Output is correct
28 Correct 1 ms 896 KB Output is correct
29 Correct 1 ms 896 KB Output is correct
30 Correct 1 ms 896 KB Output is correct
31 Correct 1 ms 1024 KB Output is correct
32 Correct 1 ms 1024 KB Output is correct
33 Correct 6 ms 3968 KB Output is correct
34 Correct 6 ms 3968 KB Output is correct
35 Correct 23 ms 3968 KB Output is correct
36 Correct 7 ms 4480 KB Output is correct
37 Correct 8 ms 4608 KB Output is correct
38 Correct 42 ms 4600 KB Output is correct
39 Correct 14 ms 4992 KB Output is correct
40 Correct 9 ms 4992 KB Output is correct
41 Correct 49 ms 5112 KB Output is correct
42 Correct 11 ms 5504 KB Output is correct
43 Correct 10 ms 5504 KB Output is correct
44 Correct 54 ms 5632 KB Output is correct
45 Correct 13 ms 6016 KB Output is correct
46 Correct 11 ms 6016 KB Output is correct
47 Correct 62 ms 6144 KB Output is correct
48 Correct 16 ms 6656 KB Output is correct
49 Correct 15 ms 6652 KB Output is correct
50 Correct 109 ms 6656 KB Output is correct
51 Correct 17 ms 7168 KB Output is correct
52 Correct 17 ms 7168 KB Output is correct
53 Correct 91 ms 7168 KB Output is correct
54 Correct 19 ms 7552 KB Output is correct
55 Correct 19 ms 7680 KB Output is correct
56 Correct 108 ms 7680 KB Output is correct
57 Correct 27 ms 8064 KB Output is correct
58 Correct 23 ms 8064 KB Output is correct
59 Correct 157 ms 8192 KB Output is correct
60 Correct 26 ms 8704 KB Output is correct
61 Correct 23 ms 8576 KB Output is correct
62 Correct 209 ms 8696 KB Output is correct
63 Correct 127 ms 8568 KB Output is correct
64 Correct 165 ms 8696 KB Output is correct
65 Correct 188 ms 8696 KB Output is correct
66 Correct 148 ms 8696 KB Output is correct
67 Correct 131 ms 8568 KB Output is correct
68 Correct 53 ms 8696 KB Output is correct
69 Correct 55 ms 8696 KB Output is correct
70 Correct 43 ms 8696 KB Output is correct
71 Correct 46 ms 8696 KB Output is correct
72 Correct 36 ms 8696 KB Output is correct
73 Correct 38 ms 8808 KB Output is correct
74 Correct 69 ms 8800 KB Output is correct
75 Correct 73 ms 8800 KB Output is correct
76 Correct 80 ms 8800 KB Output is correct
77 Correct 84 ms 8800 KB Output is correct
78 Correct 83 ms 8700 KB Output is correct
79 Correct 73 ms 8828 KB Output is correct
80 Correct 81 ms 8700 KB Output is correct
81 Correct 92 ms 8708 KB Output is correct
82 Correct 77 ms 8708 KB Output is correct
83 Correct 103 ms 8736 KB Output is correct
84 Correct 87 ms 8736 KB Output is correct
85 Correct 85 ms 8736 KB Output is correct
86 Correct 114 ms 8816 KB Output is correct
87 Correct 105 ms 8736 KB Output is correct
88 Correct 119 ms 8916 KB Output is correct
89 Correct 110 ms 8796 KB Output is correct
90 Correct 140 ms 8788 KB Output is correct
91 Correct 129 ms 8788 KB Output is correct
92 Correct 131 ms 8788 KB Output is correct