結果

問題 No.2366 登校
ユーザー i_am_noobi_am_noob
提出日時 2023-06-30 22:17:05
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 197 ms / 4,000 ms
コード長 1,758 bytes
コンパイル時間 2,166 ms
コンパイル使用メモリ 209,568 KB
実行使用メモリ 23,296 KB
最終ジャッジ日時 2024-06-12 07:50:16
合計ジャッジ時間 4,527 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using pii=pair<int,int>;

#define all(a) a.begin(),a.end()
#define pb push_back
#define sz(a) ((int)a.size())

const int N=85,dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int n,m,k,t,c[N][N],d[N][N];
ll dis[N][N][N*4];
bool vis[N][N][N*4];

signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    cin >> n >> m >> k >> t;
    if(t>=n+m-2){
        cout << "0\n";
        return 0;
    }
    for(int i=0; i<k; ++i){
        int x,y; cin >> x >> y; x--,y--;
        cin >> c[x][y] >> d[x][y];
    }
    for(int i=0; i<n; ++i) for(int j=0; j<m; ++j) for(int cur=0; cur<=N*2+n+m; ++cur) dis[i][j][cur]=4e18;
    using T=pair<ll,array<int,3>>;
    priority_queue<T,vector<T>,greater<T>> pq;
    dis[0][0][N*2+t]=0,pq.push({0,{0,0,N*2+t}});
    while(!pq.empty()){
        auto [_,arr]=pq.top(); pq.pop();
        auto [x,y,cur]=arr;
        if(vis[x][y][cur]) continue;
        //cout << x << ' ' << y << ' ' << cur << endl;
        vis[x][y][cur]=1;
        if(cur-(n-1-x)-(m-1-y)>=N*2){
            cout << dis[x][y][cur] << "\n";
            return 0;
        }
        if(cur>0){
            for(int i=0; i<4; ++i){
                int nx=x+dx[i],ny=y+dy[i];
                if(nx>=0&&nx<n&&ny>=0&&ny<m&&dis[x][y][cur]<dis[nx][ny][cur-1]){
                    dis[nx][ny][cur-1]=dis[x][y][cur];
                    pq.push({dis[nx][ny][cur-1],{nx,ny,cur-1}});
                }
            }
        }
        if(c[x][y]>0){
            int ncur=min(cur+c[x][y]-1,N*2+(n+m-1));
            if(dis[x][y][cur]+d[x][y]<dis[x][y][ncur]){
                dis[x][y][ncur]=dis[x][y][cur]+d[x][y];
                pq.push({dis[x][y][ncur],{x,y,ncur}});
            }
        }
    }
    cout << "-1\n";
}
0