/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 38ms 532.0 KiB
#3 Accepted 38ms 596.0 KiB
#4 Accepted 42ms 648.0 KiB
#5 Accepted 66ms 640.0 KiB
#6 Accepted 125ms 65.73 MiB
#7 Accepted 125ms 65.543 MiB
#8 Accepted 127ms 65.52 MiB
#9 Accepted 153ms 65.52 MiB
#10 Accepted 125ms 65.566 MiB
#11 Accepted 127ms 65.52 MiB
#12 Accepted 126ms 65.52 MiB
#13 Accepted 126ms 65.578 MiB
#14 Accepted 130ms 65.52 MiB
#15 Accepted 154ms 65.672 MiB
#16 Accepted 125ms 65.52 MiB
#17 Accepted 148ms 65.52 MiB
#18 Accepted 129ms 65.668 MiB
#19 Accepted 130ms 65.52 MiB
#20 Accepted 131ms 65.52 MiB
#21 Accepted 131ms 65.52 MiB
#22 Accepted 131ms 65.523 MiB
#23 Accepted 128ms 65.52 MiB
#24 Accepted 128ms 65.52 MiB
#25 Accepted 132ms 65.52 MiB
#26 Accepted 133ms 65.52 MiB
#27 Accepted 130ms 65.523 MiB
#28 Accepted 131ms 65.52 MiB
#29 Accepted 132ms 65.602 MiB
#30 Accepted 130ms 65.703 MiB
#31 Accepted 127ms 65.703 MiB
#32 Accepted 132ms 65.664 MiB
#33 Accepted 128ms 65.52 MiB
#34 Accepted 131ms 65.516 MiB
#35 Accepted 152ms 65.672 MiB
#36 Accepted 132ms 65.52 MiB
#37 Accepted 134ms 65.594 MiB
#38 Accepted 132ms 65.562 MiB
#39 Accepted 135ms 65.52 MiB
#40 Accepted 159ms 65.66 MiB
#41 Accepted 138ms 65.52 MiB
#42 Accepted 156ms 65.562 MiB
#43 Accepted 125ms 65.594 MiB
#44 Accepted 124ms 65.539 MiB
#45 Accepted 128ms 65.75 MiB
#46 Accepted 125ms 65.52 MiB
#47 Accepted 127ms 65.578 MiB
#48 Accepted 126ms 65.566 MiB
#49 Accepted 131ms 65.52 MiB
#50 Accepted 129ms 65.52 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fr freopen("input49.txt","r",stdin);
//ofstream file("output49.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-30 09:04:28
Judged At
2024-12-30 09:04:28
Judged By
Score
100
Total Time
159ms
Peak Memory
65.75 MiB