QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#352035#5476. Remodeling the Dungeonwarner1129#TL 0ms3824kbC++204.1kb2024-03-12 19:48:432024-03-12 19:48:45

Judging History

你现在查看的是最新测评结果

  • [2024-03-12 19:48:45]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3824kb
  • [2024-03-12 19:48:43]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

template <ranges::range T,
          class = enable_if_t<!is_convertible_v<T, string_view>>>
istream &operator>>(istream &s, T &&v) {
    for (auto &&x : v)
        s >> x;
    return s;
}
template <ranges::range T,
          class = enable_if_t<!is_convertible_v<T, string_view>>>
ostream &operator<<(ostream &s, T &&v) {
    for (auto &&x : v)
        s << x << ' ';
    return s;
}

#ifdef LOCAL
template <class... T> void dbg(T... x) {
    char e{};
    ((cerr << e << x, e = ' '), ...);
}
#define debug(x...) dbg(#x, '=', x, '\n')
#else
#define debug(...) ((void)0)
#endif

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define ff first
#define ss second

template <class T> inline constexpr T inf = numeric_limits<T>::max() / 2;
template <class T> bool chmin(T &a, T b) { return (b < a and (a = b, true)); }
template <class T> bool chmax(T &a, T b) { return (a < b and (a = b, true)); }

using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

constexpr i64 mod = 1e9 + 7;

void solve() {
    int n, m;
    cin >> n >> m;

    auto getid = [&](int x, int y) {
        return x * m + y;
    };

    vector<pair<int, int>> edg;

    vector G(n * m, vector<int>{});
    
    for (int i = 0; i < n; i++) {
        string L[2];
        
        cin >> L[0];
        cin >> L[1];
        
        for (int j = 0; j < m; j++) {
            int p = 2 * j + 1;
             
            if (j) {
                if (L[1][p - 1] == '|') {
                    edg.emplace_back(getid(i, j), getid(i, j - 1));
                } else {
                    G[getid(i, j)].push_back(getid(i, j - 1));
                    G[getid(i, j - 1)].push_back(getid(i, j));
                }
            }
            
            if (j + 1 < m) {
                if (L[1][p + 1] == '|') {
                    edg.emplace_back(getid(i, j), getid(i, j + 1));
                } else {
                    G[getid(i, j)].push_back(getid(i, j + 1));
                    G[getid(i, j + 1)].push_back(getid(i, j));
                }
            }

            if (i) {
                if (L[0][p] == '-') {
                    edg.emplace_back(getid(i, j), getid(i - 1, j));
                } else {
                    G[getid(i, j)].push_back(getid(i - 1, j));
                    G[getid(i - 1, j)].push_back(getid(i, j));
                }
            }
        }
    }
    {
        string line;
        cin >> line;
    }

    auto bfs = [&](int s) -> vector<int> {
        vector<int> dis(n * m, 0);
        dis[s] = 1;
        queue<int> que;
        que.push(s);
        while (!que.empty()) {
            int u = que.front();
            que.pop();
            for (int v : G[u]) if (dis[v] == 0) {
                dis[v] = dis[u] + 1;
                que.push(v);
            }
        }
        return dis;
    };

    auto disS = bfs(0);
    auto disT = bfs(getid(n - 1, m - 1));
    debug(disS);

    vector<int> bel(n * m);
    vector<bool> mark(n * m);
    auto pa = [&]() {
        vector<int> pa(n * m);
        auto dfs = [&](auto self, int u, int f) -> void {
            pa[u] = f;
            for (int v : G[u]) if (v != f) {
                self(self, v, u);
            }
        };
        dfs(dfs, 0, -1);
        return pa;
    }();

    for (int p = getid(n - 1, m - 1); p != -1; p = pa[p]) {
        mark[p] = 1;
    }

    auto dfs = [&](auto self, int u, int b) -> void {
        if (mark[u]) b = u;
        bel[u] = b;
        for (int v : G[u]) if (v != pa[u]) {
            self(self, v, b);
        }
    };
    dfs(dfs, 0, -1);

    int ans = disS[getid(n - 1, m - 1)];
    for (auto [a, b] : edg) {
        if (bel[a] != bel[b]) {
            chmax(ans, min(disS[a] + disT[b], disS[b] + disT[a]));
        }
    }

    cout << ans << '\n';
    
}

signed main() {
    cin.tie(0)->sync_with_stdio(false);
    cin.exceptions(cin.failbit);
    int T = 1;
    // cin >> T;
    while (T--)
        solve();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3824kb

input:

2 3
+-+-+-+
|.....|
+.+.+.+
|.|.|.|
+-+-+-+

output:

6

result:

ok single line: '6'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3792kb

input:

2 3
+-+-+-+
|...|.|
+.+.+.+
|.|...|
+-+-+-+

output:

4

result:

ok single line: '4'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3632kb

input:

5 5
+-+-+-+-+-+
|...|...|.|
+-+.+.+.+.+
|...|.|.|.|
+.+.+.+-+.+
|.|...|.|.|
+.+.+-+.+.+
|.|.....|.|
+-+.+.+-+.+
|...|.....|
+-+-+-+-+-+

output:

15

result:

ok single line: '15'

Test #4:

score: -100
Time Limit Exceeded

input:

500 500
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-...

output:


result: