답안 #511171

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
511171 2022-01-15T10:30:23 Z 600Mihnea Mecho (IOI09_mecho) C++17
76 / 100
193 ms 15920 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 = 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;
          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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 460 KB Output is correct
7 Correct 87 ms 15732 KB Output is correct
8 Correct 0 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 0 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 972 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 0 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 972 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 1100 KB Output is correct
28 Correct 1 ms 1100 KB Output is correct
29 Correct 1 ms 1228 KB Output is correct
30 Correct 1 ms 1228 KB Output is correct
31 Correct 1 ms 1228 KB Output is correct
32 Correct 1 ms 1356 KB Output is correct
33 Correct 6 ms 5324 KB Output is correct
34 Correct 7 ms 5452 KB Output is correct
35 Incorrect 18 ms 5564 KB Output isn't correct
36 Correct 8 ms 6216 KB Output is correct
37 Correct 8 ms 6216 KB Output is correct
38 Incorrect 27 ms 6500 KB Output isn't correct
39 Correct 9 ms 7016 KB Output is correct
40 Correct 9 ms 6980 KB Output is correct
41 Incorrect 30 ms 7360 KB Output isn't correct
42 Correct 10 ms 7880 KB Output is correct
43 Correct 10 ms 7880 KB Output is correct
44 Incorrect 38 ms 8260 KB Output isn't correct
45 Correct 13 ms 8644 KB Output is correct
46 Correct 12 ms 8716 KB Output is correct
47 Incorrect 62 ms 9284 KB Output isn't correct
48 Correct 16 ms 9536 KB Output is correct
49 Correct 14 ms 9604 KB Output is correct
50 Incorrect 59 ms 10304 KB Output isn't correct
51 Correct 18 ms 10436 KB Output is correct
52 Correct 18 ms 10436 KB Output is correct
53 Incorrect 70 ms 11268 KB Output isn't correct
54 Correct 19 ms 11328 KB Output is correct
55 Correct 19 ms 11332 KB Output is correct
56 Incorrect 86 ms 12312 KB Output isn't correct
57 Correct 22 ms 12236 KB Output is correct
58 Correct 22 ms 12276 KB Output is correct
59 Incorrect 101 ms 13308 KB Output isn't correct
60 Correct 31 ms 13308 KB Output is correct
61 Correct 24 ms 13248 KB Output is correct
62 Incorrect 128 ms 14512 KB Output isn't correct
63 Correct 115 ms 13664 KB Output is correct
64 Correct 179 ms 13740 KB Output is correct
65 Correct 193 ms 13716 KB Output is correct
66 Correct 137 ms 13704 KB Output is correct
67 Correct 119 ms 13652 KB Output is correct
68 Correct 63 ms 13652 KB Output is correct
69 Correct 58 ms 13736 KB Output is correct
70 Correct 58 ms 13720 KB Output is correct
71 Correct 50 ms 13644 KB Output is correct
72 Correct 52 ms 13616 KB Output is correct
73 Correct 50 ms 15912 KB Output is correct
74 Correct 71 ms 15892 KB Output is correct
75 Correct 64 ms 15912 KB Output is correct
76 Correct 76 ms 15916 KB Output is correct
77 Correct 67 ms 15920 KB Output is correct
78 Correct 79 ms 15900 KB Output is correct
79 Correct 61 ms 15824 KB Output is correct
80 Correct 62 ms 15824 KB Output is correct
81 Correct 66 ms 15916 KB Output is correct
82 Correct 62 ms 15880 KB Output is correct
83 Correct 85 ms 15808 KB Output is correct
84 Correct 74 ms 15784 KB Output is correct
85 Correct 74 ms 15792 KB Output is correct
86 Correct 81 ms 15784 KB Output is correct
87 Correct 73 ms 15780 KB Output is correct
88 Correct 86 ms 15724 KB Output is correct
89 Correct 83 ms 15744 KB Output is correct
90 Correct 89 ms 15720 KB Output is correct
91 Correct 91 ms 15664 KB Output is correct
92 Correct 94 ms 15724 KB Output is correct