結果

問題 No.2366 登校
ユーザー 👑 potato167potato167
提出日時 2023-06-30 23:07:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 869 ms / 4,000 ms
コード長 2,893 bytes
コンパイル時間 2,509 ms
コンパイル使用メモリ 223,108 KB
実行使用メモリ 45,908 KB
最終ジャッジ日時 2023-09-03 03:00:40
合計ジャッジ時間 8,748 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 229 ms
13,636 KB
testcase_13 AC 223 ms
13,768 KB
testcase_14 AC 227 ms
13,680 KB
testcase_15 AC 232 ms
13,724 KB
testcase_16 AC 232 ms
13,644 KB
testcase_17 AC 230 ms
13,648 KB
testcase_18 AC 225 ms
13,580 KB
testcase_19 AC 229 ms
13,652 KB
testcase_20 AC 226 ms
13,636 KB
testcase_21 AC 224 ms
13,676 KB
testcase_22 AC 201 ms
13,740 KB
testcase_23 AC 472 ms
45,908 KB
testcase_24 AC 468 ms
45,628 KB
testcase_25 AC 869 ms
45,716 KB
testcase_26 AC 862 ms
45,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const int mod=1e9+7;
#define rep(i,a,b) for (ll i=a;i<b;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}



void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,0,t) solve();
}

void solve(){
	ll N,M,K,T;
    cin>>N>>M>>K>>T;
    int B=5;
    ll L=B*(N+M)+1;
    int S=2*(N+M);
    T+=S;
    chmin(T,L-1);
    vector dp(N,vector(M,vector<ll>(L,ILL)));
    dp[0][0][S]=0;
    _pq<pair<ll,tuple<ll,ll,ll>>> pq;
    pq.push({0,{0,0,S}});
    vector<vector<ll>> C(N,vector<ll>(M,-1));
    auto D=C;
    rep(i,0,K){
        ll a,b,c,d;
        cin>>a>>b>>c>>d;
        a--,b--;
        c--;
        if(c==0) continue;
        C[a][b]=c;
        D[a][b]=d;
    }
    vector<int> dx={1,-1,0,0,0},dy={0,0,1,-1,0};
    while(!pq.empty()){
        ll cost,x,y,z;
        cost=pq.top().first;
        tie(x,y,z)=pq.top().second;
        pq.pop();
        if(dp[x][y][z]!=cost) continue;
        //cout<<x<<" "<<y<<" "<<z<<" "<<cost<<endl;
        if(z!=L-1){
            rep(k,0,5){
                int s=dx[k]+x,t=dy[k]+y;
                if(s==-1||t==-1||s==N||t==M) continue;
                if(chmin(dp[s][t][z+1],dp[x][y][z])) pq.push({cost,{s,t,z+1}});
            }
        }
        if(C[x][y]==-1) continue;
        ll n_z=max(0ll,z-C[x][y]);
        cost+=D[x][y];
        if(chmin(dp[x][y][n_z],cost)) pq.push({cost,{x,y,n_z}});
    }
    cout<<(dp[N-1][M-1][T]==ILL?-1:dp[N-1][M-1][T])<<"\n";
}
0