/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 32ms 576.0 KiB
#3 Time Exceeded ≥2104ms ≥65.316 MiB
#4 Time Exceeded ≥2102ms ≥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'

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<<endl: cout<<ans<<endl;
}
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-31 20:34:45
Judged At
2024-12-31 20:34:45
Judged By
Score
2
Total Time
≥2104ms
Peak Memory
≥65.523 MiB