/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 39ms 532.0 KiB
#3 Accepted 125ms 65.52 MiB
#4 Accepted 146ms 65.52 MiB
#5 Accepted 124ms 65.52 MiB
#6 Accepted 58ms 532.0 KiB
#7 Accepted 42ms 536.0 KiB
#8 Accepted 66ms 580.0 KiB
#9 Accepted 130ms 65.566 MiB
#10 Accepted 141ms 65.52 MiB
#11 Accepted 131ms 65.695 MiB
#12 Accepted 128ms 65.621 MiB
#13 Accepted 130ms 65.727 MiB
#14 Accepted 132ms 65.703 MiB
#15 Accepted 155ms 65.527 MiB
#16 Accepted 154ms 65.52 MiB
#17 Accepted 127ms 65.527 MiB
#18 Accepted 131ms 65.746 MiB
#19 Accepted 128ms 65.52 MiB
#20 Accepted 153ms 65.52 MiB
#21 Accepted 133ms 65.715 MiB
#22 Accepted 132ms 65.566 MiB
#23 Accepted 132ms 65.52 MiB
#24 Accepted 135ms 65.52 MiB
#25 Accepted 131ms 65.562 MiB
#26 Accepted 131ms 65.57 MiB
#27 Accepted 132ms 65.52 MiB
#28 Accepted 133ms 65.645 MiB
#29 Accepted 133ms 65.566 MiB
#30 Accepted 147ms 65.574 MiB
#31 Accepted 133ms 65.68 MiB
#32 Accepted 133ms 65.746 MiB
#33 Accepted 131ms 65.629 MiB
#34 Accepted 148ms 65.582 MiB
#35 Accepted 136ms 65.711 MiB
#36 Accepted 133ms 65.656 MiB
#37 Accepted 157ms 65.52 MiB
#38 Accepted 131ms 65.746 MiB
#39 Accepted 147ms 65.52 MiB
#40 Accepted 165ms 65.52 MiB
#41 Accepted 136ms 65.562 MiB
#42 Accepted 164ms 65.668 MiB
#43 Accepted 163ms 65.52 MiB
#44 Accepted 135ms 65.723 MiB
#45 Accepted 128ms 65.566 MiB
#46 Accepted 127ms 65.629 MiB
#47 Accepted 127ms 65.52 MiB
#48 Accepted 124ms 65.52 MiB
#49 Accepted 129ms 65.52 MiB
#50 Accepted 130ms 65.566 MiB
#51 Accepted 131ms 65.512 MiB
#52 Accepted 127ms 65.52 MiB
#53 Accepted 126ms 65.504 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fr freopen("input52.txt","r",stdin);
//ofstream file("output52.txt");
char a[2001][2001];
int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    //fr;
    int t;  cin >> t; while(t--)
    {
        ll n,m,x,y; cin >> n >> m >> x >> y;
        vector<vector<pair<ll,ll>>> dp(n+2,vector<pair<ll,ll>>(m+2,{1e18,1e18}));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++) cin >> a[i][j];
        }
        dp[1][1] = {0,0};
        for(int j=2;j<=m;j++) 
        {
            if(a[1][j]=='#') break;
            dp[1][j] = {dp[1][j-1].first + x , dp[1][j-1].first+y};
        }
        for(ll i=2;i<=n;i++)
        {
            for(ll j=1;j<=m;j++)
            {
                if(a[i][j]=='#') continue;
                dp[i][j].first = min(dp[i][j-1].first+x , dp[i-1][j].second+y);
                dp[i][j].second = min(dp[i][j-1].first+y , dp[i-1][j].second+x);
            }
            for(ll j=m;j>=1;j--)
            {
                if(a[i][j]=='#') continue;
                dp[i][j].first = min({dp[i][j].first , dp[i][j+1].first+x, dp[i-1][j].second+y});
                dp[i][j].second = min({dp[i][j].second , dp[i][j+1].first+y , dp[i-1][j].second+x});
            }
        }
        ll ans = min(dp[n][m-1].first , dp[n-1][m].second);
        (ans>=1e18)? cout<<-1<<'\n': cout<<ans<<'\n';
    }
}

Information

Submit By
Type
Submission
Problem
P1156 Low cost water management
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-31 20:33:26
Judged At
2024-12-31 20:33:26
Judged By
Score
100
Total Time
165ms
Peak Memory
65.746 MiB