結果
問題 | No.2366 登校 |
ユーザー |
|
提出日時 | 2023-06-30 22:04:27 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 299 ms / 4,000 ms |
コード長 | 1,429 bytes |
コンパイル時間 | 875 ms |
コンパイル使用メモリ | 83,840 KB |
実行使用メモリ | 19,788 KB |
最終ジャッジ日時 | 2024-06-12 07:49:19 |
合計ジャッジ時間 | 3,149 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 25 |
ソースコード
#include<iostream> #include<map> #include<queue> #include<cassert> using namespace std; int N,M,K,T; int C[80][80],D[80][80]; long dp[80][80][320]; const int d[5]={0,1,0,-1}; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin>>N>>M>>K>>T; for(int i=0;i<N;i++)for(int j=0;j<M;j++)C[i][j]=-1; for(int i=0;i<K;i++) { int a,b,c,d; cin>>a>>b>>c>>d; a--,b--; C[a][b]=c; D[a][b]=d; } if((N-1)+(M-1)<=T) { cout<<0<<endl; return 0; } const int off=(N-1)+(M-1); for(int i=0;i<N;i++)for(int j=0;j<M;j++)for(int t=0;t<=off*2;t++)dp[i][j][t]=1e18; dp[0][0][off]=0; priority_queue<pair<long,pair<int,int> > >Q; Q.push(make_pair(0,make_pair(0,off))); long ans=1e18; while(!Q.empty()) { long c=-Q.top().first; int xy=Q.top().second.first,t=Q.top().second.second; Q.pop(); int x=xy/M,y=xy%M; if(dp[x][y][t]<c)continue; if(x==N-1&&y==M-1) { if(t<=off+T)ans=min(ans,c); if(C[x][y]!=-1&&t-C[x][y]<=off+T)ans=min(ans,c+D[x][y]); } if(C[x][y]!=-1) { int nt=max(t-C[x][y]+1,0); long nc=c+D[x][y]; if(dp[x][y][nt]>nc) { dp[x][y][nt]=nc; Q.push(make_pair(-nc,make_pair(x*M+y,nt))); } } for(int r=0;r<4;r++) { int tx=x+d[r],ty=y+d[r+1]; if(tx<0||ty<0||tx>=N||ty>=M||t+1>off*2)continue; if(dp[tx][ty][t+1]>c) { dp[tx][ty][t+1]=c; Q.push(make_pair(-c,make_pair(tx*M+ty,t+1))); } } } if(ans==(long)1e18)ans=-1; cout<<ans<<endl; }