/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Wrong Answer 52ms 580.0 KiB
#3 Accepted 96ms 7.871 MiB
#4 Accepted 96ms 7.816 MiB
#5 Accepted 96ms 8.02 MiB
#6 Wrong Answer 51ms 580.0 KiB

Code

#include <bits/stdc++.h>

#define int long long

void solve() {
    int m, n;
    std::cin >> m >> n;
    int x, y;
    std::cin >> x >> y;
    std::vector<std::string> g(m);
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            char c;
            std::cin >> c;
            g[i] += c;
        }
    }
    std::vector<int> dp(n, LLONG_MAX);
    dp[0] = 0;
    for (int i = 1; i < n; i++) {
        if (g[0][i] != '#') {
            dp[i] = x * (i - 1LL) + y;
        } else {
            break;
        }
    }
    for (int i = 1; i < m - 1; i++) {
        std::vector<int> ndp(n, LLONG_MAX);
        std::vector<int> suf(n, LLONG_MAX), pre(n, LLONG_MAX);
        if (dp[n - 1] != LLONG_MAX && g[i][n - 1] != '#') {
            suf[n - 1] = dp[n - 1] + x * (n - 2LL);
        }
        for (int j = n - 2; j > 0; j--) {
            if (dp[j] == LLONG_MAX) {
                suf[j] = suf[j + 1];
                continue;
            }
            if (g[i][j] == '#') {
                continue;
            }
            suf[j] = std::min(suf[j + 1], dp[j] + x * (j - 1LL));
        }
        if (dp[0] != LLONG_MAX && g[i][0] != '#') {
            pre[0] = dp[0] + x * (n - 2LL);
        }
        for (int j = 1; j < n - 1; j++) {
            if (dp[j] == LLONG_MAX) {
                pre[j] = pre[j - 1];
                continue;
            }
            if (g[i][j] == '#') {
                continue;
            }
            pre[j] = std::min(pre[j - 1], dp[j] + x * (n - j - 2LL));
        }
        for (int j = 0; j < n; j++) {
            if (g[i][j] == '#') {
                continue;
            }
            if (dp[j] != LLONG_MAX) {
                ndp[j] = dp[j] + x;
            }
            if (j > 0 && pre[j - 1] != LLONG_MAX) {
                ndp[j] = std::min(ndp[j], pre[j - 1] - x * (n - j - 1LL) + 2LL * y);
            }
            if (j < n - 1 && suf[j + 1] != LLONG_MAX) {
                ndp[j] = std::min(ndp[j], suf[j + 1] - x * j + 2LL * y);
            }
        }
        std::swap(dp, ndp);
    }
    int ans = dp[n - 1];
    for (int j = n - 2; j >= 0; j--) {
        if (g[m - 1][j] == '#') {
            break;
        }
        if (dp[j] == LLONG_MAX) {
            continue;
        }
        ans = std::min(ans, dp[j] + y + x * (n - j - 2LL));
    }
    if (ans == LLONG_MAX) {
        std::cout << -1 << "\n";
    } else {
        std::cout << ans << "\n";
    }
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    int t;
    std::cin >> t;
    while (t--) {
        solve();
    }
}

Information

Submit By
Type
Submission
Problem
P1156 Low cost water management
Contest
Happy New Year 2025
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 16:38:04
Judged At
2025-01-02 16:38:04
Judged By
Score
4
Total Time
96ms
Peak Memory
8.02 MiB