/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 584.0 KiB
#2 Accepted 789ms 788.0 KiB
#3 Time Exceeded ≥2079ms ≥96.605 MiB
#4 Time Exceeded ≥2052ms ≥96.445 MiB

Code

#include <bits/stdc++.h>

using namespace std;

// #include "debug.h"

#define int int64_t

const int inf = 1e16;

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

        vector<vector<char>> s(n, vector<char>(m));
        for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                        cin >> s[i][j];
                }
        }

        if (s[0][0] == '#') {
                cout << -1 << '\n';
                return;
        }

        vector<vector<array<int, 3>>> dp(n, vector<array<int, 3>>(m, {inf, inf, inf}));

        if (s[0][1] != '#') {
                dp[0][1][0] = 0;
        }
        if (s[1][0] != '#') {
                dp[1][0][2] = 0;
        }

        priority_queue<array<int, 4>> Q;
        Q.push({0, 0, 1, 0});
        Q.push({0, 1, 0, 2});

        while (!Q.empty()) {
                auto [_, i, j, k] = Q.top();
                Q.pop();

                if (s[i][j] == '#') {
                        continue;
                }

                if (k == 0) {
                        // down or right
                        if (i + 1 < n && dp[i + 1][j][2] > dp[i][j][k] + y) {
                                dp[i + 1][j][2] = dp[i][j][k] + y;
                                Q.push({dp[i + 1][j][2], i + 1, j, 2});
                        }
                        if (j + 1 < m && dp[i][j + 1][0] > dp[i][j][k] + x) {
                                dp[i][j + 1][0] = dp[i][j][k] + x;
                                Q.push({dp[i][j + 1][0], i, j + 1, 0});
                        }
                } else if (k == 1) {
                        // down or left
                        if (i + 1 < n && dp[i + 1][j][2] > dp[i][j][k] + y) {
                                dp[i + 1][j][2] = dp[i][j][k] + y;
                                Q.push({dp[i + 1][j][2], i + 1, j, 2});
                        }
                        if (j - 1 >= 0 && dp[i][j - 1][1] > dp[i][j][k] + x) {
                                dp[i][j - 1][1] = dp[i][j][k] + x;
                                Q.push({dp[i][j - 1][1], i, j - 1, 1});
                        }
                } else {
                        // down, left, right
                        if (i + 1 < n && dp[i + 1][j][2] > dp[i][j][k] + x) {
                                dp[i + 1][j][2] = dp[i][j][k] + x;
                                Q.push({dp[i + 1][j][2], i + 1, j, 2});
                        }
                        if (j - 1 >= 0 && dp[i][j - 1][1] > dp[i][j][k] + y) {
                                dp[i][j - 1][1] = dp[i][j][k] + y;
                                Q.push({dp[i][j - 1][1], i, j - 1, 1});
                        }
                        if (j + 1 < m && dp[i][j + 1][0] > dp[i][j][k] + y) {
                                dp[i][j + 1][0] = dp[i][j][k] + y;
                                Q.push({dp[i][j + 1][0], i, j + 1, 0});
                        }
                }
        }

        int ans = min(dp[n - 1][m - 1][0], dp[n - 1][m - 1][2]);

        cout << (ans >= inf ? -1 : ans) << '\n';
}

int32_t main() {
        ios_base::sync_with_stdio(false);
        cin.tie(0);

        int tc = 1;
        cin >> tc;

        while (tc--) {
                solve();
        }
}

Information

Submit By
Type
Submission
Problem
P1156 Low cost water management
Contest
Happy New Year 2025
Language
C++11 (G++ 13.2.0)
Submit At
2025-01-02 16:54:09
Judged At
2025-01-02 16:54:09
Judged By
Score
2
Total Time
≥2079ms
Peak Memory
≥96.605 MiB