結果

問題 No.2366 登校
ユーザー HIcoderHIcoder
提出日時 2023-07-05 12:48:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 606 ms / 4,000 ms
コード長 3,129 bytes
コンパイル時間 1,338 ms
コンパイル使用メモリ 122,524 KB
実行使用メモリ 20,592 KB
最終ジャッジ日時 2023-09-03 03:01:23
合計ジャッジ時間 5,744 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 178 ms
7,804 KB
testcase_13 AC 166 ms
7,800 KB
testcase_14 AC 160 ms
7,916 KB
testcase_15 AC 175 ms
7,896 KB
testcase_16 AC 167 ms
7,848 KB
testcase_17 AC 171 ms
7,744 KB
testcase_18 AC 171 ms
7,852 KB
testcase_19 AC 177 ms
7,896 KB
testcase_20 AC 170 ms
8,040 KB
testcase_21 AC 161 ms
7,852 KB
testcase_22 AC 141 ms
7,496 KB
testcase_23 AC 108 ms
20,516 KB
testcase_24 AC 109 ms
20,468 KB
testcase_25 AC 606 ms
20,592 KB
testcase_26 AC 602 ms
20,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<ll,ll> P;
typedef pair<int,P> PP;
const ll MOD=998244353;

const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};

int main(){
    int N,M,K,T;
    cin>>N>>M>>K>>T;

    vector<vector<P>> back(N,vector<P>(M,make_pair(0,0)));
    for(int i=0;i<K;i++){
        int a,b;
        ll c,d;
        cin>>a>>b>>c>>d;
        a--;b--;
        back[a][b]=make_pair(c,d);
    }

    ll offset=N+M+3;

    vector dp(N,vector<vector<ll>>(M,vector<ll>(2*(N+M)+10,INF)));

    typedef tuple<ll,ll,ll,ll> TP;

    priority_queue<TP,vector<TP>,greater<TP>> pq;

    pq.emplace(0,0,0,0);

    while(!pq.empty()){

        auto [val,nowi,nowj,nowt] = pq.top();//valは負の値.ちいさいほうから
        pq.pop();

        //ll cost=-val;
        ll cost=val;

        
        if(dp[nowi][nowj][nowt+offset]<=cost) continue;

        //dp[nowi][nowj][nowt+offset]>cost
        dp[nowi][nowj][nowt+offset]=cost;


        for(int k=0;k<4;k++){
            int ni=nowi+dy[k];
            int nj=nowj+dx[k];

            if(ni<0 || N<=ni || nj<0 || M<=nj) continue;
            ll totime=nowt+1;

            if(totime+offset<2*(N+M)+10){
                if(dp[ni][nj][totime+offset]>cost ){
                    pq.emplace(cost,ni,nj,totime);
                }
            }
        }


        if(back[nowi][nowj]!=make_pair(0LL,0LL)){
                auto [ci,di]=back[nowi][nowj];
                ll totime = nowt-ci+1;
                if(totime<-(N+M-2)){
                    totime=-(N+M-2);
                } 

                if(dp[nowi][nowj][totime+offset]>(cost+di) ){
                    pq.emplace((cost+di),nowi,nowj,totime);
                }
        }


        /*
        if(nowi+1<N){
            int ni=nowi+1;
            int nj=nowj;

            if(back[nowi][nowj]==make_pair(0LL,0LL)){
                if(dp[ni][nj][nowt+1+offset]>cost ){
                    pq.emplace(cost,ni,nj,nowt+1);
                }
            
            }
        }

        if(nowj+1<M){
            int ni=nowi;
            int nj=nowj+1;

            if(back[nowi][nowj]==make_pair(0LL,0LL)){
                if(dp[ni][nj][nowt+1+offset]>cost ){
                    pq.emplace(cost,ni,nj,nowt+1);
                }
            
            }
        }


        if(back[nowi][nowj]!=make_pair(0LL,0LL)){  
            auto [ci,di]=back[nowi][nowj];
            ll totime = nowt-ci+1;
            if(totime<-(N+M-2)){
                totime=-(N+M-2);
            } 

            if(dp[nowi][nowj][totime+offset]>(cost+di) ){
                pq.emplace((cost+di),nowi,nowj,totime);
            }

        }
        */


      


    }

    ll ans=INF;
    /*
    for(int t=0;t<2*(N+M)+10 && t<=T-offset;t++){
        ans=min(ans,dp[N-1][M-1][t]);
    }
    */

    for(int t=0;t<2*(N+M)+10 && t<=T+offset;t++){
        ans=min(ans,dp[N-1][M-1][t]);
    }
    
    cout<<(ans==INF?-1:ans)<<endl;

}
0