Submission #1088634

# Submission time Handle Problem Language Result Execution time Memory
1088634 2024-09-14T17:27:16 Z v_matei Mecho (IOI09_mecho) C++17
74 / 100
210 ms 13340 KB
#include <bits/stdc++.h>
#include <functional>
#include <queue>
#include <vector>

#define ll long long
#define ull unsigned long long
#define pii std::pair<int, int>

#define IO (std::string) ""
std::ifstream fin(IO + ".in");
std::ofstream fout(IO + ".out");

#define NMAX 1000
#define INF INT_MAX

int n, s;
char map[NMAX][NMAX];
pii start, finish;
std::vector<pii> hives;
std::vector<std::vector<int>> d1(NMAX, std::vector<int>(NMAX, INF)),
    d2(NMAX, std::vector<int>(NMAX, INF));

int di[]{1, -1, 0, 0}, dj[]{0, 0, 1, -1};

void citire() {
  std::cin >> n >> s;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      std::cin >> map[i][j];
      if (map[i][j] == 'M')
        start = {i, j};
      if (map[i][j] == 'D')
        finish = {i, j};
      if (map[i][j] == 'H')
        hives.push_back({i, j});
    }
}

bool in(pii coord) {
  int i = coord.first, j = coord.second;
  return i >= 0 && i < n && j >= 0 && j < n;
}

void bfs(std::queue<pii> &q, std::vector<std::vector<int>> &d,
         std::function<bool(pii coords)> check) {
  while (!q.empty()) {
    pii u = q.front();
    int i = u.first;
    int j = u.second;
    q.pop();

    for (int k = 0; k < 4; k++) {
      int ni = i + di[k];
      int nj = j + dj[k];
      if (check({ni, nj}) && d[ni][nj] > d[i][j] + 1) {
        d[ni][nj] = d[i][j] + 1;
        q.push({ni, nj});
      }
    }
  }
}
void bfs2(std::queue<pii> &q, std::vector<std::vector<int>> &d,
          std::function<bool(pii coords)> check) {
  while (!q.empty()) {
    pii u = q.front();
    int i = u.first;
    int j = u.second;
    q.pop();

    for (int k = 0; k < 4; k++) {
      int ni = i + di[k];
      int nj = j + dj[k];
      if (check({ni, nj}) && d[ni][nj] > d[i][j] + 1) {
        if (d[i][j] + 1 < s * d2[ni][nj]) {
          d[ni][nj] = d[i][j] + 1;
          q.push({ni, nj});
        }
      }
    }
  }
}

int b_search(int l, int r, std::function<bool(int)> f) {
  while (r > l + 1) {
    int mid = (r + l) / 2;
    if (f(mid))
      l = mid;
    else
      r = mid;
  }
  return l;
}

int ok = 0;

int main() {
  std::cin.tie(0)->std::ios::sync_with_stdio(0);
  citire();

  std::queue<pii> q;

  for (pii it : hives) {
    q.push(it);
    d2[it.first][it.second] = 0;
  }

  bfs(q, d2, [&](pii coords) {
    return in(coords) && map[coords.first][coords.second] != 'D' &&
           map[coords.first][coords.second] != 'T';
  });
  int ans = b_search(0, n * n, [&](int w) {
    std::vector<std::vector<int>> d(NMAX, std::vector<int>(NMAX, INF));
    d[start.first][start.second] = w * s;
    q.push(start);
    if (w >= d2[start.first][start.second])
      q.pop();
    bfs2(q, d, [&](pii coords) {
      return in(coords) && map[coords.first][coords.second] != 'H' &&
             map[coords.first][coords.second] != 'T';
    });

    for (int k = 0; k < 4; k++) {
      if (in({finish.first + di[k], finish.second + dj[k]}) &&
          d[finish.first + di[k]][finish.second + dj[k]] != INF) {
        ok = 1;
        return true;
      }
    }
    return false;
  });

  if (ok) {
    std::cout << ans << '\n';
  } else
    std::cout << -1 << '\n';
  /*for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (d2[i][j] != INF)
        std::cout << d2[i][j] << ' ';
      else
        std::cout << "oo ";
    }
    std::cout << '\n';
  }*/
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12184 KB Output is correct
2 Correct 9 ms 12200 KB Output is correct
3 Correct 9 ms 12204 KB Output is correct
4 Correct 10 ms 12212 KB Output is correct
5 Incorrect 13 ms 12220 KB Output isn't correct
6 Correct 12 ms 12244 KB Output is correct
7 Correct 113 ms 13192 KB Output is correct
8 Correct 13 ms 12244 KB Output is correct
9 Correct 13 ms 12264 KB Output is correct
10 Correct 14 ms 12340 KB Output is correct
11 Correct 14 ms 12260 KB Output is correct
12 Incorrect 19 ms 12400 KB Output isn't correct
13 Incorrect 17 ms 12380 KB Output isn't correct
14 Correct 17 ms 12412 KB Output is correct
15 Correct 15 ms 12284 KB Output is correct
16 Correct 14 ms 12280 KB Output is correct
17 Correct 14 ms 12284 KB Output is correct
18 Correct 14 ms 12280 KB Output is correct
19 Correct 15 ms 12304 KB Output is correct
20 Correct 15 ms 12304 KB Output is correct
21 Correct 16 ms 12304 KB Output is correct
22 Correct 16 ms 12340 KB Output is correct
23 Correct 17 ms 12380 KB Output is correct
24 Correct 18 ms 12388 KB Output is correct
25 Correct 18 ms 12400 KB Output is correct
26 Correct 18 ms 12396 KB Output is correct
27 Correct 18 ms 12672 KB Output is correct
28 Correct 19 ms 12428 KB Output is correct
29 Correct 17 ms 12416 KB Output is correct
30 Correct 20 ms 12432 KB Output is correct
31 Correct 18 ms 12416 KB Output is correct
32 Correct 19 ms 12420 KB Output is correct
33 Correct 28 ms 12716 KB Output is correct
34 Correct 26 ms 12696 KB Output is correct
35 Correct 45 ms 12696 KB Output is correct
36 Correct 27 ms 12736 KB Output is correct
37 Correct 29 ms 12776 KB Output is correct
38 Correct 50 ms 12828 KB Output is correct
39 Correct 28 ms 12856 KB Output is correct
40 Correct 30 ms 12876 KB Output is correct
41 Correct 63 ms 12740 KB Output is correct
42 Correct 32 ms 12952 KB Output is correct
43 Correct 33 ms 12904 KB Output is correct
44 Correct 72 ms 12860 KB Output is correct
45 Correct 40 ms 12988 KB Output is correct
46 Correct 35 ms 12872 KB Output is correct
47 Correct 84 ms 12868 KB Output is correct
48 Correct 33 ms 12968 KB Output is correct
49 Correct 35 ms 12960 KB Output is correct
50 Correct 95 ms 12952 KB Output is correct
51 Correct 36 ms 13000 KB Output is correct
52 Correct 43 ms 12980 KB Output is correct
53 Correct 111 ms 13004 KB Output is correct
54 Correct 40 ms 13092 KB Output is correct
55 Correct 40 ms 13060 KB Output is correct
56 Correct 126 ms 13056 KB Output is correct
57 Correct 42 ms 13160 KB Output is correct
58 Correct 46 ms 13136 KB Output is correct
59 Correct 152 ms 13084 KB Output is correct
60 Correct 44 ms 13160 KB Output is correct
61 Correct 45 ms 13208 KB Output is correct
62 Correct 148 ms 13128 KB Output is correct
63 Incorrect 115 ms 13176 KB Output isn't correct
64 Correct 210 ms 13048 KB Output is correct
65 Correct 156 ms 13060 KB Output is correct
66 Correct 140 ms 13056 KB Output is correct
67 Correct 137 ms 13316 KB Output is correct
68 Incorrect 69 ms 13240 KB Output isn't correct
69 Correct 67 ms 13188 KB Output is correct
70 Correct 63 ms 13200 KB Output is correct
71 Correct 60 ms 13192 KB Output is correct
72 Correct 56 ms 13340 KB Output is correct
73 Correct 54 ms 13264 KB Output is correct
74 Correct 78 ms 13244 KB Output is correct
75 Correct 92 ms 13196 KB Output is correct
76 Correct 91 ms 13212 KB Output is correct
77 Correct 96 ms 13136 KB Output is correct
78 Correct 102 ms 13136 KB Output is correct
79 Correct 73 ms 13260 KB Output is correct
80 Correct 82 ms 13200 KB Output is correct
81 Correct 99 ms 13196 KB Output is correct
82 Correct 84 ms 13208 KB Output is correct
83 Incorrect 101 ms 13156 KB Output isn't correct
84 Correct 99 ms 13200 KB Output is correct
85 Correct 111 ms 13224 KB Output is correct
86 Correct 107 ms 13164 KB Output is correct
87 Correct 101 ms 13200 KB Output is correct
88 Incorrect 110 ms 13144 KB Output isn't correct
89 Correct 120 ms 13140 KB Output is correct
90 Correct 130 ms 13132 KB Output is correct
91 Correct 108 ms 13224 KB Output is correct
92 Correct 110 ms 13136 KB Output is correct