/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 123ms 632.0 KiB
#3 Accepted 554ms 218.742 MiB
#4 Accepted 563ms 218.812 MiB
#5 Accepted 555ms 218.812 MiB
#6 Accepted 122ms 632.0 KiB
#7 Accepted 93ms 636.0 KiB
#8 Accepted 95ms 532.0 KiB
#9 Accepted 546ms 218.703 MiB
#10 Accepted 553ms 218.77 MiB
#11 Accepted 551ms 218.695 MiB
#12 Accepted 551ms 218.77 MiB
#13 Accepted 555ms 218.77 MiB
#14 Accepted 379ms 218.625 MiB
#15 Accepted 547ms 218.703 MiB
#16 Accepted 572ms 218.801 MiB
#17 Accepted 554ms 218.738 MiB
#18 Accepted 549ms 218.703 MiB
#19 Accepted 548ms 218.703 MiB
#20 Accepted 533ms 218.812 MiB
#21 Accepted 550ms 218.77 MiB
#22 Accepted 549ms 218.699 MiB
#23 Accepted 376ms 218.52 MiB
#24 Accepted 376ms 218.648 MiB
#25 Accepted 539ms 218.77 MiB
#26 Accepted 536ms 218.703 MiB
#27 Accepted 535ms 218.77 MiB
#28 Accepted 531ms 218.953 MiB
#29 Accepted 533ms 218.77 MiB
#30 Accepted 536ms 218.949 MiB
#31 Accepted 542ms 218.953 MiB
#32 Accepted 532ms 218.77 MiB
#33 Accepted 538ms 218.953 MiB
#34 Accepted 540ms 218.703 MiB
#35 Accepted 549ms 218.945 MiB
#36 Accepted 529ms 218.953 MiB
#37 Accepted 550ms 218.77 MiB
#38 Accepted 536ms 218.949 MiB
#39 Accepted 384ms 218.566 MiB
#40 Accepted 386ms 218.562 MiB
#41 Accepted 545ms 218.859 MiB
#42 Accepted 393ms 218.52 MiB
#43 Accepted 528ms 218.77 MiB
#44 Accepted 539ms 218.953 MiB
#45 Accepted 566ms 218.77 MiB
#46 Accepted 565ms 218.711 MiB
#47 Accepted 573ms 218.77 MiB
#48 Accepted 550ms 218.695 MiB
#49 Accepted 558ms 218.77 MiB
#50 Accepted 554ms 218.77 MiB
#51 Accepted 560ms 218.84 MiB
#52 Accepted 562ms 218.77 MiB
#53 Accepted 551ms 218.699 MiB

Code

#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif

using namespace std;
#define int long long
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';

vector<vector<pair<pair<int,int>,int> > > dir{
    {{{1, 0}, 0}, {{0, -1}, 1}, {{0, 1}, 2}},
    {{{1, 0}, 0}, {{0, -1}, 1}},
    {{{1, 0}, 0}, {{0, 1}, 2}},
};

const int INF = 1e15;

void shelby() {
    int n, m;
    cin >> n >> m;
    array<int, 2> cost;
    vector<vector<char> > mat(n, vector<char>(m));
    cin >> cost[0] >> cost[1];
    for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> mat[i][j];
    vector memo(n, vector(m, vector<int>(3, -1)));
    auto dp = [&](auto &&self, int x, int y, int k, int d = 0)-> int {
        debug(x, y, k, d);
        if (x >= n || y >= m || x < 0 || y < 0 || mat[x][y] == '#') return INF;
        if (x == n - 1 && y == m - 1) return 0;
        int &ret = memo[x][y][k];
        if (~ret) return ret;
        ret = INF;
        for (auto &it: dir[k]) {
            int x1 = x + it.first.first, y1 = y + it.first.second;
            ret = min(ret, cost[k != it.second] + self(self, x1, y1, it.second, d + 1));
        }
        return ret;
    };

    int res = min(dp(dp, 0, 1, 2), dp(dp, 1, 0, 0));
    if (res == INF) res = -1;
    cout << res << '\n';
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        // cout << "Case " << _ << ": ";
        shelby();
    }
}

Information

Submit By
Type
Submission
Problem
P1156 Low cost water management
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-12 18:14:57
Judged At
2025-01-12 18:14:57
Judged By
Score
100
Total Time
573ms
Peak Memory
218.953 MiB