/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 211ms 532.0 KiB
#3 Accepted 1034ms 341.453 MiB
#4 Accepted 1029ms 341.43 MiB
#5 Accepted 1003ms 341.453 MiB
#6 Accepted 213ms 532.0 KiB
#7 Accepted 213ms 648.0 KiB
#8 Accepted 211ms 532.0 KiB
#9 Accepted 1015ms 341.531 MiB
#10 Accepted 1017ms 341.52 MiB
#11 Accepted 997ms 341.531 MiB
#12 Accepted 1016ms 341.301 MiB
#13 Accepted 999ms 341.52 MiB
#14 Accepted 1001ms 341.535 MiB
#15 Accepted 989ms 341.352 MiB
#16 Accepted 995ms 341.332 MiB
#17 Accepted 1015ms 341.402 MiB
#18 Accepted 1023ms 341.391 MiB
#19 Accepted 998ms 341.309 MiB
#20 Accepted 993ms 341.523 MiB
#21 Accepted 999ms 341.48 MiB
#22 Accepted 999ms 341.332 MiB
#23 Accepted 985ms 341.438 MiB
#24 Accepted 1020ms 341.379 MiB
#25 Accepted 1015ms 341.52 MiB
#26 Accepted 983ms 341.492 MiB
#27 Accepted 1012ms 341.457 MiB
#28 Accepted 1005ms 341.512 MiB
#29 Accepted 1008ms 341.445 MiB
#30 Accepted 992ms 341.309 MiB
#31 Accepted 1266ms 341.52 MiB
#32 Accepted 993ms 341.312 MiB
#33 Accepted 993ms 341.52 MiB
#34 Accepted 1002ms 341.492 MiB
#35 Accepted 989ms 341.406 MiB
#36 Accepted 986ms 341.414 MiB
#37 Accepted 1035ms 341.348 MiB
#38 Accepted 994ms 341.531 MiB
#39 Accepted 1009ms 341.531 MiB
#40 Accepted 1009ms 341.457 MiB
#41 Accepted 1001ms 341.406 MiB
#42 Accepted 1024ms 341.535 MiB
#43 Accepted 1008ms 341.469 MiB
#44 Accepted 992ms 341.531 MiB
#45 Accepted 1017ms 341.441 MiB
#46 Accepted 995ms 341.438 MiB
#47 Accepted 997ms 341.504 MiB
#48 Accepted 1021ms 341.324 MiB
#49 Accepted 998ms 341.426 MiB
#50 Accepted 999ms 341.484 MiB
#51 Accepted 995ms 341.469 MiB
#52 Accepted 1002ms 341.523 MiB
#53 Accepted 1025ms 341.336 MiB

Code

#include <bits/stdc++.h>

#ifndef ONLINE_JUDGE
// #include "template.cpp"
#else
#define debug(...)
#define debugArr(...)
#endif

#define F first
#define S second
#define int long long
#define double long double
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define mp(x, y) make_pair(x, y)
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define INF (int)1e17
using namespace std;

using pii = pair<int, int>;
using pdd = pair<double, double>;
using pbb = pair<bool, bool>;
using pcc = pair<char, char>;
using pss = pair<string, string>;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vpii = vector<pair<int, int>>;
using vpdd = vector<pair<double, double>>;
using vpbb = vector<pair<bool, bool>>;
using vpcc = vector<pair<char, char>>;
using vpss = vector<pair<string, string>>;
using vvi = vector<vector<int>>;
using vvd = vector<vector<double>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvs = vector<vector<string>>;
using vvpii = vector<vector<pair<int, int>>>;
using vvpdd = vector<vector<pair<double, double>>>;
using vvpbb = vector<vector<pair<bool, bool>>>;
using vvpcc = vector<vector<pair<char, char>>>;
using vvpss = vector<vector<pair<string, string>>>;

// clang-format off
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T> T sqrt_(T elem) {int l=1,r=elem;while(l<=r){int mid=l+(r-l)/2LL;if(mid>elem/mid){r=mid-1;}else{int val=mid*mid;if(val<=elem){l=mid+1;}else{r=mid-1;}}}return r;}
template <class T> T ceil_(T a,T b) {return(a+b-1)/b;};
template <class T> T mod_add(T a,T b,T mod){return((a%mod)+(b%mod))% mod;}
template <class T> T mod_sub(T a,T b,T mod){return((a%mod)-(b%mod)+mod)%mod;}
template <class T> T mod_mul(T a,T b,T mod){return((a%mod)*(b%mod))%mod;}
template <class T> T mod_inv(T a,T mod){T m0=mod,y=0,x=1;if(mod==1)return 0;while(a>1){T q=a/mod;T t=mod;mod=a%mod,a=t;t=y;y=x-q*y;x=t;}if(x<0)x+=m0;return x;}
template <class T> T mod_div(T a,T b,T mod){return mod_mul(a,mod_inv(b,mod),mod);}
// clang-format on
void solve() {
  int n, m, x, y;
  cin >> n >> m >> x >> y;

  vvc a(n + 1, vc(m + 1));

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      cin >> a[i][j];
    }
  }

  vector<vvi> dp(n + 1, vvi(m + 1, vi(6, INF)));

  auto init = [&](int x, int y) -> void {
    for (int i = 0; i < 6; ++i) {
      dp[x][y][i] = 0;
    }
  };

  init(1, 1);
  init(n, m);

  for (int i = 1; i <= n; ++i) {

    vvi l(m + 1, vi(6, INF));
    vvi r(m + 1, vi(6, INF));

    if (i == 1) {
      for (int k = 0; k < 6; ++k) {
        l[1][k] = 0;
        r[1][k] = 0;
      }
    }

    for (int j = 1; j <= m; ++j) {

      if (a[i][j] == '#') {
        continue;
      }

      for (int k = 0; k < 6; ++k) {
        if (k == 0 || k == 2 || k == 3) {
          if (i - 1 >= 1) {
            int v1 = dp[i - 1][j][0];
            int v2 = dp[i - 1][j][4];
            int v3 = dp[i - 1][j][5];
            int add = (k == 0 ? x : y);
            r[j][k] = min(r[j][k], min({v1, v2, v3}) + add);
          }
        } else if (k == 1 || k == 5) {
          if (j - 1 >= 1) {
            int add = (k == 1 ? x : y);
            int v1 = r[j - 1][1];
            int v2 = r[j - 1][2];
            r[j][k] = min(r[j][k], min(v1, v2) + add);
          }
        }
      }
    }

    for (int j = m; j >= 1; --j) {

      if (a[i][j] == '#') {
        continue;
      }

      for (int k = 0; k < 6; ++k) {

        if (k == 0 || k == 2 || k == 3) {
          if (i - 1 >= 1) {
            int v1 = dp[i - 1][j][0];
            int v2 = dp[i - 1][j][4];
            int v3 = dp[i - 1][j][5];
            int add = (k == 0 ? x : y);
            l[j][k] = min(l[j][k], min({v1, v2, v3}) + add);
          }
        } else if (k == 1 || k == 4) {
          if (j + 1 <= m) {
            int add = (k == 1 ? x : y);
            int v1 = l[j + 1][1];
            int v2 = l[j + 1][3];
            l[j][k] = min(l[j][k], min(v1, v2) + add);
          }
        }
      }
    }

    // debug(l, r);

    for (int j = 1; j <= m; ++j) {
      for (int k = 0; k < 6; ++k) {
        dp[i][j][k] = min({dp[i][j][k], l[j][k], r[j][k]});
      }
    }
  }

  int ans =
      min({dp[n - 1][m][0], dp[n - 1][m][5], dp[n][m - 1][1], dp[n][m - 1][2]});
  if (ans >= INF) {
    ans = -1;
  }
  cout << ans << '\n';
  return;
}

signed main() {

  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int tt = 1, count = 1;
  cin >> tt;
  while (tt--) {
    // cout << "Case #" << count << ": ";
    solve();
    ++count;
  }
  return 0;
}

Information

Submit By
Type
Submission
Problem
P1156 Low cost water management
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-03 06:36:38
Judged At
2025-01-03 06:36:38
Judged By
Score
100
Total Time
1266ms
Peak Memory
341.535 MiB