結果

問題 No.2366 登校
ユーザー t98slidert98slider
提出日時 2023-06-30 23:01:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 4,000 ms
コード長 4,186 bytes
コンパイル時間 1,833 ms
コンパイル使用メモリ 184,988 KB
実行使用メモリ 11,700 KB
最終ジャッジ日時 2023-09-03 03:00:22
合計ジャッジ時間 3,245 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 35 ms
5,568 KB
testcase_13 AC 10 ms
4,376 KB
testcase_14 AC 13 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 23 ms
4,784 KB
testcase_18 AC 19 ms
4,520 KB
testcase_19 AC 31 ms
5,300 KB
testcase_20 AC 8 ms
4,380 KB
testcase_21 AC 9 ms
4,380 KB
testcase_22 AC 19 ms
5,164 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 4 ms
4,376 KB
testcase_25 AC 75 ms
11,444 KB
testcase_26 AC 76 ms
11,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;

template<class T> istream& operator >> (istream& is, vector<T>& vec) {
    for(T& x : vec) is >> x;
    return is;
}

template<class T> ostream& operator << (ostream& os, const vector<T>& vec) {
    if(vec.empty()) return os;
    os << vec[0];
    for(auto it = vec.begin(); ++it != vec.end(); ) os << ' ' << *it;
    return os;
}

/*int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w, k, t;
    cin >> h >> w >> k >> t;
    if(t >= h + w){
        cout << 0 << '\n';
        return 0;
    }
    vector<vector<ll>> C(h, vector<ll>(w)), D(h, vector<ll>(w));
    for(int i = 0; i < k; i++){
        int y, x, v1, v2;
        cin >> y >> x >> v1 >> v2;
        y--, x--;
        C[y][x] = v1;
        D[y][x] = v2;
    }
    const ll INF = 1ll << 60;
    //時間, y, x
    vector<vector<vector<ll>>> dptb(h + w, vector<vector<ll>>(h, vector<ll>(w, INF)));
    vector<rpriority_queue<tuple<ll, int, int>>> pqtb(h + w);
    dptb[0][0][0] = 0;
    pqtb[0].emplace(0, 0, 0);
    ll ans = INF;
    for(int hv = 0; hv < h + w; hv++){
        auto &pq = pqtb[hv];
        auto &dp = dptb[hv];
        while(!pq.empty()){
            ll d;
            int y, x;
            tie(d, y, x) = pq.top();
            pq.pop();
            if(d > dp[y][x]) continue;
            if(C[y][x] && hv + D[y][x] < h + w){
                int hv2 = hv + D[y][x];
                if(d - C[y][x] + 1 <= dptb[hv2][y][x]){
                    dptb[hv2][y][x] = d - C[y][x] + 1;
                    pqtb[hv2].emplace(dptb[hv2][y][x], y, x);
                }
            }
            for(int i = 0; i < 4; i++){
                int ny = y + (i == 0) - (i == 1);
                int nx = x + (i == 2) - (i == 3);
                if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
                if(d + 1 >= dp[ny][nx]) continue;
                dp[ny][nx] = d + 1;
                pq.emplace(d + 1, ny, nx);
            }
        }
        for(int y = 0; y < h; y++){
            cerr << dp[y] << '\n';
        }
        cerr << '\n';
        if(dp[h - 1][w - 1] <= t){
            cout << hv << '\n';
            return 0;
        }
    }
    cout << -1 << '\n';
}*/

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h, w, k, t;
    cin >> h >> w >> k >> t;
    if(t >= h + w - 2){
        cout << 0 << '\n';
        return 0;
    }
    vector<vector<ll>> C(h, vector<ll>(w)), D(h, vector<ll>(w));
    for(int i = 0; i < k; i++){
        int y, x, v1, v2;
        cin >> y >> x >> v1 >> v2;
        y--, x--;
        C[y][x] = v1;
        D[y][x] = v2;
    }
    int dec = h + w - 2 - t;
    const ll INF = 1ll << 60;
    //時間, y, x = 疲労
    vector<vector<vector<ll>>> dp(dec, vector<vector<ll>>(h, vector<ll>(w, INF)));
    rpriority_queue<tuple<ll, ll, int, int>> pq;
    dp[0][0][0] = 0;
    pq.emplace(0, 0, 0, 0);
    ll ans = INF;
    while(!pq.empty()){
        ll tim, d;
        int y, x;
        tie(d, tim, y, x) = pq.top();
        pq.pop();
        if(d > dp[tim][y][x]) continue;
        if(C[y][x] >= 2){
            int nt = tim + C[y][x] - 1;
            if(nt < dec){
                if(d + D[y][x] < dp[nt][y][x]){
                    dp[nt][y][x] = d + D[y][x];
                    pq.emplace(dp[nt][y][x], nt, y, x);
                }
            }else{
                ans = min(ans, d + D[y][x]);
            }
        }
        for(int i = 0; i < 4; i++){
            int ny = y + (i == 0) - (i == 1);
            int nx = x + (i == 2) - (i == 3);
            if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
            if(ny < y || nx < x){
                int nt = tim - 2;
                if(nt < 0) continue;
                if(d >= dp[nt][ny][nx]) continue;
                dp[nt][ny][nx] = d;
                pq.emplace(d, nt, ny, nx);
            }else{
                if(d >= dp[tim][ny][nx]) continue;
                dp[tim][ny][nx] = d;
                pq.emplace(d, tim, ny, nx);
            }
        }
    }
    if(ans == INF) ans = -1;
    cout << ans << '\n';
}
0