Submission #513892

# Submission time Handle Problem Language Result Execution time Memory
513892 2022-01-17T22:42:12 Z nhuang685 Mecho (IOI09_mecho) C++17
84 / 100
194 ms 6668 KB
#include <bits/stdc++.h>

#ifdef VSLOCAL
#include "debug/d.h"
#endif
#ifdef LOCAL
// Don't use this for USACO
#include "../../debug/d.h"
#endif

using namespace std;

using ll = long long;
using ld = long double;
#if defined(LOCAL) || defined(VSLOCAL)
#define dbg(...) line_info(__LINE__, #__VA_ARGS__), dbg1(__VA_ARGS__)
void nline() { cerr << '\n'; }
#else
#define dbg(...) 0
void nline() {}
#endif

int main() {
    ios::sync_with_stdio(0); cin.tie(0);

    #ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("log.txt", "w", stderr);
    #endif

    const array<int, 4> dx {-1, 0, 1, 0}, dy {0, 1, 0, -1};

    int N, S;
    cin >> N >> S;
    vector<vector<char>> grid (N, vector<char> (N));
    queue<pair<ll, pair<int, int>>> q;
    vector<vector<ll>> beetime (N, vector<ll> (N, (ll)4e18));
    vector<vector<bool>> visited (N, vector<bool> (N));
    pair<int, int> honey, home;

    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            cin >> grid[i][j];
            if (grid[i][j] == 'H') { 
                q.push({0, {i, j}}); 
                visited[i][j] = true;
                beetime[i][j] = 0;
            }
            else if (grid[i][j] == 'M') honey = {i, j};
            else if (grid[i][j] == 'D') home = {i, j};
        }
    }

    while (!q.empty()) {
        auto [dist, loc] = q.front();
        q.pop();

        for (int d = 0; d < 4; ++d) {
            pair<int, int> nloc = {loc.first + dx[d], loc.second + dy[d]};
            if (nloc.first < 0 || nloc.first >= N || nloc.second < 0 || nloc.second >= N || visited[nloc.first][nloc.second]) continue;
            if (grid[nloc.first][nloc.second] == 'G' || grid[nloc.first][nloc.second] == 'M') {
                visited[nloc.first][nloc.second] = true;
                beetime[nloc.first][nloc.second] = dist + S;
                q.push({dist + S, nloc});
            }
        }
    }
    dbg(beetime);

    auto good = [&](ll val) {
        visited.assign(N, vector<bool> (N));
        visited[honey.first][honey.second] = true;
        while (!q.empty()) q.pop();
        q.push({val * S, honey});
        while (!q.empty()) {
            auto [dist, loc] = q.front();
            q.pop();

            for (int d = 0; d < 4; ++d) {
                pair<int, int> nloc = {loc.first + dx[d], loc.second + dy[d]};
                if (nloc.first < 0 || nloc.first >= N || nloc.second < 0 || nloc.second >= N 
                    || visited[nloc.first][nloc.second] || dist + 1 >= beetime[nloc.first][nloc.second]) continue;
                if (grid[nloc.first][nloc.second] == 'D') {
                    return true;
                }
                else if (grid[nloc.first][nloc.second] == 'G') {
                    visited[nloc.first][nloc.second] = true;
                    q.push({dist + 1, nloc});
                }
            }
        }
        return false;
    };

    ll low = 0, high = 1e12;
    while (low < high) {
        ll mid = (low + high + 1) / 2;
        if (good(mid)) low = mid;
        else high = mid - 1;
    }
    
    if (good(low)) cout << low << '\n';
    else cout << "-1\n";
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:19:18: warning: statement has no effect [-Wunused-value]
   19 | #define dbg(...) 0
      |                  ^
mecho.cpp:68:5: note: in expansion of macro 'dbg'
   68 |     dbg(beetime);
      |     ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 97 ms 6348 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Incorrect 1 ms 332 KB Output isn't correct
13 Incorrect 1 ms 332 KB Output isn't correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 0 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 0 ms 204 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 204 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 5 ms 1456 KB Output is correct
34 Correct 5 ms 1356 KB Output is correct
35 Correct 23 ms 1356 KB Output is correct
36 Correct 5 ms 1740 KB Output is correct
37 Correct 5 ms 1740 KB Output is correct
38 Correct 31 ms 1740 KB Output is correct
39 Correct 6 ms 2124 KB Output is correct
40 Correct 6 ms 2124 KB Output is correct
41 Correct 37 ms 2124 KB Output is correct
42 Correct 11 ms 2620 KB Output is correct
43 Correct 12 ms 2508 KB Output is correct
44 Correct 58 ms 2600 KB Output is correct
45 Correct 12 ms 3020 KB Output is correct
46 Correct 11 ms 3084 KB Output is correct
47 Correct 64 ms 3080 KB Output is correct
48 Correct 13 ms 3532 KB Output is correct
49 Correct 12 ms 3604 KB Output is correct
50 Correct 72 ms 3532 KB Output is correct
51 Correct 13 ms 4172 KB Output is correct
52 Correct 13 ms 4172 KB Output is correct
53 Correct 97 ms 4172 KB Output is correct
54 Correct 19 ms 4684 KB Output is correct
55 Correct 19 ms 4684 KB Output is correct
56 Correct 114 ms 4784 KB Output is correct
57 Correct 20 ms 5432 KB Output is correct
58 Correct 21 ms 5324 KB Output is correct
59 Correct 122 ms 5440 KB Output is correct
60 Correct 22 ms 6092 KB Output is correct
61 Correct 21 ms 6108 KB Output is correct
62 Correct 135 ms 6140 KB Output is correct
63 Correct 115 ms 6132 KB Output is correct
64 Correct 183 ms 6120 KB Output is correct
65 Correct 194 ms 6120 KB Output is correct
66 Correct 135 ms 6136 KB Output is correct
67 Correct 123 ms 6092 KB Output is correct
68 Correct 54 ms 6136 KB Output is correct
69 Correct 58 ms 6152 KB Output is correct
70 Correct 49 ms 6092 KB Output is correct
71 Correct 38 ms 6132 KB Output is correct
72 Incorrect 36 ms 6196 KB Output isn't correct
73 Incorrect 50 ms 6664 KB Output isn't correct
74 Correct 74 ms 6604 KB Output is correct
75 Correct 73 ms 6604 KB Output is correct
76 Correct 68 ms 6668 KB Output is correct
77 Correct 74 ms 6604 KB Output is correct
78 Correct 87 ms 6612 KB Output is correct
79 Correct 60 ms 6620 KB Output is correct
80 Correct 72 ms 6608 KB Output is correct
81 Correct 88 ms 6604 KB Output is correct
82 Correct 64 ms 6604 KB Output is correct
83 Correct 84 ms 6536 KB Output is correct
84 Correct 91 ms 6536 KB Output is correct
85 Correct 93 ms 6596 KB Output is correct
86 Correct 87 ms 6532 KB Output is correct
87 Correct 87 ms 6476 KB Output is correct
88 Correct 84 ms 6444 KB Output is correct
89 Correct 95 ms 6440 KB Output is correct
90 Correct 100 ms 6348 KB Output is correct
91 Correct 100 ms 6348 KB Output is correct
92 Correct 94 ms 6348 KB Output is correct