/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Wrong Answer 181ms 744.0 KiB
#3 Time Exceeded ≥2095ms ≥12.926 MiB

Code

#include <bits/stdc++.h>
using namespace std;

//template
#pragma region 
const int MOD = 1e9 + 7;

#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;

template<class T> using ordset = tree<
T,
null_type,
less<T>,
rb_tree_tag,
tree_order_statistics_node_update>;


struct mi {
    int v;
    explicit operator int() const { return v; }
    mi() { v = 0; }
    mi(long long _v) : v(_v % MOD) { v += (v < 0) * MOD; }
};
mi &operator+=(mi &a, mi b) {
    if ((a.v += b.v) >= MOD) a.v -= MOD;
    return a;
}
mi &operator-=(mi &a, mi b) {
    if ((a.v -= b.v) < 0) a.v += MOD;
    return a;
}
mi operator+(mi a, mi b) { return a += b; }
mi operator-(mi a, mi b) { return a -= b; }
mi operator*(mi a, mi b) { return mi((long long)a.v * b.v); }
mi &operator*=(mi &a, mi b) { return a = a * b; }
mi mypow(mi a, long long p) {
    assert(p >= 0);
    return p == 0 ? 1 : mypow(a * a, p / 2) * (p & 1 ? a : 1);
}
mi inv(mi a) {
    assert(a.v != 0);
    return mypow(a, MOD - 2);
}
mi operator/(mi a, mi b) { return a * inv(b); }

#define all(x) x.begin(), x.end()
#define lef(x) (x << 1)
#define rig(x) (lef(x) | 1)
#define ft first
#define sd second
#define mt make_tuple
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define dbg(x) cout << #x << " = " << x << '\n';
#define rep(i, begin, end) for (__typeof(begin) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<int, int> pii;
typedef pair<ld, ld> pdb;
// typedef __int128_t lll;

template<typename T, typename T1>
ostream & operator<<(ostream &os, pair<T, T1> p){
    os << "(" << p.ft << "," << p.sd << ")";
    return os;
}

template<typename T, typename T1>
istream & operator>>(istream &is, pair<T, T1>& p){
    is >> p.ft >> p.sd;
    return is;
}

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#pragma endregion

const int N = 1e5 + 30;
ll n, m, k;
ll x[N];
string s;
char g[2010][2010];

char vis[2010][2010][2];

bool in(int i, int j) {return i >= 0 && i < n && j >= 0 && j < m && g[i][j] != '#';}


void test() {
    ll a, b;
    cin >> n >> m;
    cin >> a >> b;

    priority_queue<pair<ll,pair<pii,int>>> q;
    rep(i,0,n)
        rep(j,0,m) cin >> g[i][j], vis[i][j][0] = 0, vis[i][j][1] = 0;

    q.push({0,{{0,0},0}});
    q.push({0,{{0,0},1}});

    ll ans = 1e9;

    while(!q.empty()){
        ll c = q.top().ft;
        auto p = q.top().sd;

        q.pop();

        if(vis[p.ft.ft][p.ft.sd][p.sd]) continue;
        vis[p.ft.ft][p.ft.sd][p.sd] = 1;

        // cout << p << ' ' << -c << endl;
        if(in(p.ft.ft + 1, p.ft.sd) && !p.sd){
            ll cust = 0;
            if(p.ft.ft + 1 != n -1 || p.ft.sd != m -1 ) cust = a;
            q.push({c - cust,{{p.ft.ft + 1, p.ft.sd}, p.sd}});
        }
        if(in(p.ft.ft, p.ft.sd + 1) && p.sd){
            ll cust = 0;
            if(p.ft.ft != n -1 || p.ft.sd + 1!= m -1 ) cust = a;
            q.push({c - cust,{{p.ft.ft, p.ft.sd + 1}, p.sd}});
        }

        if(in(p.ft.ft, p.ft.sd + 1) && p.sd){
            ll cust = 0;
            if(p.ft.ft != n -1 || p.ft.sd + 1 != m -1 ) cust = b;
            q.push({c - cust,{{p.ft.ft, p.ft.sd + 1}, p.sd^1}});
        }

        if(in(p.ft.ft + 1, p.ft.sd) && !p.sd){
            ll cust = 0;
            if(p.ft.ft + 1 != n -1 || p.ft.sd != m -1 ) cust = b;
            q.push({c - cust,{{p.ft.ft + 1, p.ft.sd}, p.sd^1}});
        }

        if(p.ft.ft == n - 1 && p.ft.sd == m - 1){
            ans = min(ans, -c);
        }
    }
    cout << (ans == 1e9 ? -1:ans)  << '\n';

}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);


    int te = 1;
    cin >> te;
    while(te--) test();
    
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1156 Low cost water management
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 17:27:29
Judged At
2025-01-02 17:27:29
Judged By
Score
1
Total Time
≥2095ms
Peak Memory
≥12.926 MiB