/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 88ms 636.0 KiB
#3 Accepted 376ms 96.457 MiB
#4 Accepted 346ms 96.434 MiB
#5 Accepted 367ms 96.43 MiB
#6 Accepted 87ms 640.0 KiB
#7 Accepted 78ms 532.0 KiB
#8 Accepted 54ms 648.0 KiB
#9 Accepted 1157ms 97.102 MiB
#10 Accepted 1253ms 97.191 MiB
#11 Accepted 971ms 97.008 MiB
#12 Accepted 1113ms 97.062 MiB
#13 Accepted 1170ms 97.121 MiB
#14 Accepted 123ms 96.199 MiB
#15 Time Exceeded ≥2102ms ≥101.938 MiB
#16 Time Exceeded ≥2088ms ≥102.016 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;
        }

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

        while (!Q.empty()) {
                auto [i, j, k] = Q.front();
                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({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({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({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({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({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({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({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:49:31
Judged At
2025-01-02 16:49:31
Judged By
Score
22
Total Time
≥2102ms
Peak Memory
≥102.016 MiB