Submission #530088

# Submission time Handle Problem Language Result Execution time Memory
530088 2022-02-24T14:48:54 Z Servus2022 Mecho (IOI09_mecho) C++14
100 / 100
193 ms 16580 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 1000 + 7;
const int INF = (int) 1e9 + 7;
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
int n;
int speed;
int r1, c1;
int r2, c2;
int a[N][N];
int dist_m[N][N];
int dist_b[N][N];
 
vector<pair<int, int>> ord;
 
void compute(int dist[N][N], vector<pair<int, int>> init) {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dist[i][j] = INF;
    }
  }
 
  queue<pair<int, int>> q;
  for (auto &it : init) {
    dist[it.first][it.second] = 0;
    q.push(it);
  }
  while (!q.empty()) {
    auto it = q.front();
    int r = it.first;
    int c = it.second;
    ord.push_back(it);
    q.pop();
    for (int k = 0; k < 4; k++) {
      int rn = r + dr[k];
      int cn = c + dc[k];
      if (a[rn][cn] && dist[rn][cn] == INF) {
        dist[rn][cn] = 1 + dist[r][c];
        q.push({rn, cn});
      }
    }
  }
}
 
int dist[N][N];
bool ok[N][N];
 
signed main() {
  ios::sync_with_stdio(0); cin.tie(0);
 
///  freopen ("input", "r", stdin);
 
  cin >> n >> speed;
 
  vector<pair<int, int>> bees;
 
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= n; j++) {
      char ch = s[j - 1];
      if (ch == 'T') {
        a[i][j] = 0;
        continue;
      }
      if (ch == 'G') {
        a[i][j] = 1;
        continue;
      }
      if (ch == 'M') {
        r1 = i;
        c1 = j;
        continue;
      }
      if (ch == 'D') {
        r2 = i;
        c2 = j;
        continue;
      }
      if (ch == 'H') {
        bees.push_back({i, j});
        continue;
      }
    }
  }
  ///cout << r2 << " " << c2 << "\n";
  vector<pair<int, int>> m;
  m.push_back({r1, c1});
  a[r1][c1] = 1;
  compute(dist_b, bees);
  ord.clear();
  a[r2][c2] = 1;
  compute(dist_m, m);
 
  int low = 0, high = dist_b[r1][c1] - 1, sol = -1;
 
  while (low <= high) {
    int stay = (low + high) / 2;
    ///stay = 5;
  ///  stay = 1;
    ///stay = 3;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        ok[i][j] = 0;
      }
    }
    ok[r1][c1] = 1;
    queue<pair<int, int>> q;
    q.push({r1, c1});
    while (!q.empty()) {
      int r = q.front().first, c = q.front().second;
      q.pop();
      if (ok[r][c]) {
        for (int k = 0; k < 4; k++) {
          int rn = r + dr[k];
          int cn = c + dc[k];
          int the_dist = dist_m[r][c] + 1;
          if (ok[rn][cn] || a[rn][cn] == 0) continue;
 
          dist_m[rn][cn] = the_dist;
          ok[rn][cn] |= ((stay + the_dist / speed) < (dist_b[rn][cn] + (rn == r2 && cn == c2)));
          if (ok[rn][cn]) q.push({rn, cn});
        }
      }
    }
//    cout << " val = " << ok[r2][c2 - 1] << "\n";
 //   exit(0);
 if (0) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (ok[i][j]) {
          if ((stay + (dist_m[i][j] + speed - 1) / speed) > INF / 10 && 0) {
            cout << "? ";
          } else {
            cout << (stay + (dist_m[i][j] + speed - 1) / speed) << " ";
          }
        } else {
          cout << "- ";
        }
        ///cout << ok[i][j] << " ";
      }
      cout << "\n";
    }
    cout << "\n";
 
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (ok[i][j]) {
          if (dist_b[i][j] > INF / 10) {
            cout << "? ";
          } else {
            cout << dist_b[i][j] << " ";
          }
        } else {
          cout << "- ";
        }
        ///cout << ok[i][j] << " ";
      }
      cout << "\n";
    }
 
    exit(0);}
    if (ok[r2][c2]) {
      sol = stay;
      low = stay + 1;
    } else {
      high = stay - 1;
    }
  }
 
  cout << sol << "\n";
  return 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int val = dist_m[i][j];
      if (val == INF) {
        cout << "- ";
      } else {
        cout << val << " ";
      }
    }
    cout << "\n";
  }
 
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Correct 0 ms 460 KB Output is correct
7 Correct 97 ms 16292 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 0 ms 460 KB Output is correct
12 Correct 1 ms 1100 KB Output is correct
13 Correct 1 ms 968 KB Output is correct
14 Correct 1 ms 1100 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 716 KB Output is correct
20 Correct 1 ms 716 KB Output is correct
21 Correct 1 ms 844 KB Output is correct
22 Correct 1 ms 844 KB Output is correct
23 Correct 1 ms 964 KB Output is correct
24 Correct 1 ms 972 KB Output is correct
25 Correct 1 ms 1100 KB Output is correct
26 Correct 1 ms 1100 KB Output is correct
27 Correct 1 ms 1228 KB Output is correct
28 Correct 1 ms 1228 KB Output is correct
29 Correct 1 ms 1228 KB Output is correct
30 Correct 1 ms 1232 KB Output is correct
31 Correct 1 ms 1356 KB Output is correct
32 Correct 1 ms 1352 KB Output is correct
33 Correct 6 ms 5452 KB Output is correct
34 Correct 6 ms 5452 KB Output is correct
35 Correct 17 ms 5704 KB Output is correct
36 Correct 8 ms 6264 KB Output is correct
37 Correct 8 ms 6344 KB Output is correct
38 Correct 24 ms 6692 KB Output is correct
39 Correct 9 ms 7216 KB Output is correct
40 Correct 14 ms 7236 KB Output is correct
41 Correct 31 ms 7612 KB Output is correct
42 Correct 11 ms 8136 KB Output is correct
43 Correct 10 ms 8136 KB Output is correct
44 Correct 39 ms 8592 KB Output is correct
45 Correct 13 ms 9028 KB Output is correct
46 Correct 13 ms 9020 KB Output is correct
47 Correct 46 ms 9584 KB Output is correct
48 Correct 19 ms 9900 KB Output is correct
49 Correct 15 ms 9916 KB Output is correct
50 Correct 58 ms 10556 KB Output is correct
51 Correct 18 ms 10820 KB Output is correct
52 Correct 17 ms 10820 KB Output is correct
53 Correct 67 ms 11712 KB Output is correct
54 Correct 19 ms 11852 KB Output is correct
55 Correct 19 ms 11820 KB Output is correct
56 Correct 81 ms 12788 KB Output is correct
57 Correct 23 ms 12852 KB Output is correct
58 Correct 23 ms 12764 KB Output is correct
59 Correct 91 ms 13920 KB Output is correct
60 Correct 30 ms 13884 KB Output is correct
61 Correct 27 ms 13880 KB Output is correct
62 Correct 131 ms 15000 KB Output is correct
63 Correct 119 ms 14320 KB Output is correct
64 Correct 177 ms 14248 KB Output is correct
65 Correct 193 ms 14256 KB Output is correct
66 Correct 142 ms 14268 KB Output is correct
67 Correct 124 ms 14340 KB Output is correct
68 Correct 64 ms 14352 KB Output is correct
69 Correct 60 ms 14244 KB Output is correct
70 Correct 58 ms 14284 KB Output is correct
71 Correct 50 ms 14248 KB Output is correct
72 Correct 54 ms 14500 KB Output is correct
73 Correct 54 ms 16532 KB Output is correct
74 Correct 68 ms 16524 KB Output is correct
75 Correct 70 ms 16532 KB Output is correct
76 Correct 69 ms 16524 KB Output is correct
77 Correct 68 ms 16536 KB Output is correct
78 Correct 76 ms 16432 KB Output is correct
79 Correct 76 ms 16552 KB Output is correct
80 Correct 67 ms 16552 KB Output is correct
81 Correct 72 ms 16460 KB Output is correct
82 Correct 72 ms 16580 KB Output is correct
83 Correct 82 ms 16428 KB Output is correct
84 Correct 73 ms 16384 KB Output is correct
85 Correct 76 ms 16408 KB Output is correct
86 Correct 82 ms 16428 KB Output is correct
87 Correct 80 ms 16324 KB Output is correct
88 Correct 83 ms 16296 KB Output is correct
89 Correct 84 ms 16284 KB Output is correct
90 Correct 94 ms 16296 KB Output is correct
91 Correct 88 ms 16324 KB Output is correct
92 Correct 88 ms 16288 KB Output is correct