/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 39ms 61.996 MiB
#2 Time Exceeded ≥2089ms ≥62.066 MiB
#3 Accepted 246ms 66.34 MiB
#4 Accepted 256ms 66.273 MiB
#5 Accepted 255ms 66.266 MiB
#6 Time Exceeded ≥2075ms ≥61.77 MiB

Code

#include "bits/stdc++.h"
// #ifndef ONLINE_JUDGE
// #include "debug.h"
// #else
// #define dbg(x...)
// #endif
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
const int mod = 1000000007;
// clang-format off
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
// #define _GLIBCXX_DEBUG
// clang-format on

int dp[2005][2005][2];
int f(int i, int j, int prev, vector<vector<char>> &v, int x, int y)
{
    int n = v.size();
    int m = v[0].size();
    if ((i == n - 1 && j == m - 2 && prev == 0) || (i == n - 2 && j == m - 1 && prev == 1))
        return 0;
    if (i >= n || j >= m)
        return 1e9;
    if (v[i][j] == '#')
        return 1e9;
    if (dp[i][j][prev] != -1)
        return dp[i][j][prev];
    int p1 = 1e9, p2 = 1e9, p3 = 1e9;
    if (prev == 0)
    {
        if (j < m - 1)
            p1 = x + f(i, j + 1, 0, v, x, y);
        if (j < m - 1)
            p2 = y + f(i, j + 1, 1, v, x, y);
    }
    if (prev == 1)
    {
        if (i < n - 1)
            p1 = x + f(i + 1, j, 1, v, x, y);
        if (i < n - 1)
            p2 = y + f(i + 1, j, 0, v, x, y);
    }
    return dp[i][j][prev] = min(p1, p2);
}
void solve()
{
    ll t, m, n, a, b;
    string h;
    cin >> n >> m >> a >> b;

    vector<vector<char>> v(n, vector<char>(m));
    cin >> v;
    memset(dp, -1, sizeof(dp));
    int ans = f(0, 1, 0, v, a, b) + a;
    ans = min(ans, f(1, 0, 1, v, a, b) + a);
    ans = min(ans, f(0, 1, 1, v, a, b) + b);
    ans = min(ans, f(1, 0, 0, v, a, b) + b);
    cout << ans << endl;
}
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    // #ifndef ONLINE_JUDGE
    //     freopen("input.txt", "r", stdin);
    //     freopen("output.txt", "w", stdout);
    // #endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

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:49:49
Judged At
2025-01-02 16:49:49
Judged By
Score
4
Total Time
≥2089ms
Peak Memory
≥66.34 MiB