/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 34ms 624.0 KiB
#3 Accepted 33ms 616.0 KiB
#4 Accepted 32ms 876.0 KiB
#5 Accepted 49ms 608.0 KiB
#6 Accepted 167ms 65.449 MiB
#7 Accepted 194ms 65.523 MiB
#8 Accepted 184ms 65.52 MiB
#9 Accepted 169ms 65.512 MiB
#10 Accepted 172ms 65.523 MiB
#11 Accepted 179ms 65.742 MiB
#12 Accepted 174ms 65.363 MiB
#13 Accepted 182ms 65.535 MiB
#14 Accepted 171ms 65.285 MiB
#15 Accepted 166ms 65.516 MiB
#16 Accepted 175ms 65.453 MiB
#17 Accepted 150ms 65.449 MiB
#18 Accepted 146ms 65.371 MiB
#19 Accepted 146ms 65.441 MiB
#20 Accepted 149ms 65.676 MiB
#21 Accepted 150ms 65.371 MiB
#22 Accepted 144ms 65.668 MiB
#23 Accepted 142ms 65.27 MiB
#24 Accepted 151ms 65.285 MiB
#25 Accepted 157ms 65.527 MiB
#26 Accepted 145ms 65.523 MiB
#27 Accepted 154ms 65.508 MiB
#28 Accepted 152ms 65.32 MiB
#29 Accepted 162ms 65.703 MiB
#30 Accepted 164ms 65.52 MiB
#31 Accepted 159ms 65.637 MiB
#32 Accepted 149ms 65.344 MiB
#33 Accepted 147ms 65.461 MiB
#34 Accepted 149ms 65.441 MiB
#35 Accepted 148ms 65.391 MiB
#36 Accepted 153ms 65.352 MiB
#37 Accepted 155ms 65.516 MiB
#38 Accepted 163ms 65.371 MiB
#39 Accepted 152ms 65.504 MiB
#40 Accepted 151ms 65.312 MiB
#41 Accepted 142ms 65.645 MiB
#42 Accepted 170ms 65.352 MiB
#43 Accepted 164ms 65.5 MiB
#44 Accepted 164ms 65.461 MiB
#45 Accepted 164ms 65.578 MiB
#46 Accepted 170ms 65.52 MiB
#47 Accepted 168ms 65.453 MiB
#48 Accepted 166ms 65.691 MiB
#49 Accepted 174ms 65.465 MiB
#50 Accepted 166ms 65.523 MiB

Code

/*
 *   Copyright (c) 2024 Emon Thakur
 *   All rights reserved.
 */
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define endl '\n'
char a[2001][2001];
void solve()
{
    // string filename = "input"+to_string(ii)+".txt";
    // int nn = filename.size();
    // char fname[nn];
    // for(int i=0;i<nn;i++) fname[i] = filename[i];
    // freopen(fname,"r",stdin);
    // ofstream file("output"+to_string(ii)+".txt");
    
    //int t; cin >> t;
    int n,m; cin >> n >> m;
    ll x,y; cin >> x >> y;
    //char a[n][m];
    pair<ll,ll> dp[n][m];
    //vector<vector<int>> hasobs(n,vector<int>(m,0));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++) 
        {
            cin >> a[i][j];
            //if(a[i][j]=='#') hasobs[i][j] = 1;
            dp[i][j] = {1e18,1e18};
        }
    }
    // for(int i=0;i<n;i++)
    // {
    //     for(int j=1;j<m;j++) hasobs[i][j] = hasobs[i][j] + hasobs[i][j-1];
    // }
    dp[0][0] = {0,0};
    for(int j=1;j<m;j++) 
    {
        if(a[0][j]=='#') break;
        dp[0][j].first = dp[0][j-1].first + x;
        dp[0][j].second = dp[0][j-1].first + y;
    }
    for(ll i=1;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(a[i][j]=='#') continue;
            dp[i][j].first = min({dp[i][j].first , dp[i-1][j].second+y , (j==0)?(ll)1e18 : dp[i][j-1].first+x });
            dp[i][j].second = min({dp[i][j].second , (j==0)?(ll)1e18:dp[i][j-1].first+y , dp[i-1][j].second+x});
            ll mn = 1e18;
            for(ll k=j+1;k<m;k++)
            {
                if(a[i][k]=='#') break;
                mn = min(mn , dp[i-1][k].second + y + (k-j-1)*x);
            }
            dp[i][j].first = min(dp[i][j].first , mn + x);
            dp[i][j].second = min(dp[i][j].second , mn+y);
        }
    }
    // for(int i=0;i<n;i++)
    // {
    //     for(int j=0;j<m;j++) 
    //     {
    //         (dp[i][j].first==1e18)? cout<<"inf ": cout<<dp[i][j].first<<" ";
    //         (dp[i][j].second==1e18)? cout<<"inf  ": cout<<dp[i][j].second<<"  ";
    //     }cout<<endl;
    // }

    // edge case m==1 or n==1
    ll ans = min(dp[n-1][m-2].first , dp[n-2][m-1].second);
    (ans==1e18)? cout<<-1<<'\n': cout<<ans<<'\n';
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr);
    //for(int i=1;i<100;i++) solve(i);
    int t; cin >> t; while(t--) solve();
}

Information

Submit By
Type
Submission
Problem
P1156 Low cost water management
Language
C++17 (G++ 13.2.0)
Submit At
2024-12-30 09:06:14
Judged At
2024-12-30 09:06:14
Judged By
Score
100
Total Time
194ms
Peak Memory
65.742 MiB