結果

問題 No.2855 Move on Grid
ユーザー HIcoder
提出日時 2024-08-31 00:30:05
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,239 ms / 3,000 ms
コード長 1,886 bytes
コンパイル時間 1,445 ms
コンパイル使用メモリ 124,288 KB
最終ジャッジ日時 2025-02-24 03:28:02
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<cassert>
#include<random>
#include<set>
#include<map>
#include<cassert>
#include<unordered_map>
#include<bitset>
#include<numeric>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=1<<30;
const ll INF=1LL<<62;
typedef pair<int,ll> P;
typedef pair<int,P> PP; 
const ll MOD=998244353;
const int dy[]={0,1,0,-1};
const int dx[]={1,0,-1,0};

int main(){
    int N,M,K;
    cin>>N>>M>>K;
    vector<vector<int>> A(N,vector<int>(M));
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++){
            cin>>A[i][j];
        }
    }

    //とおる経路のうち、最小値がx以上となる経路があるか?
    //x未満の数をとおる回数がK回以下であるか?
    auto check=[&](int v){
        
        vector<vector<int>> dp(N,vector<int>(M,inf));

        //上下左右の移動がOK

        priority_queue<PP,vector<PP>,greater<PP>> pq;
        pq.push({(A[0][0]<v?1:0),P(0,0)});

        while(!pq.empty()){
            auto [score,p]=pq.top();
            pq.pop();
            
            auto [y,x]=p;

            if(dp[y][x]<=score) continue;
            //dp[y][x]>score
            dp[y][x]=score;

            for(int k=0;k<4;k++){
                int ny=y+dy[k],nx=x+dx[k];
                if(0<=ny && ny<N && 0<=nx && nx<M){
                    int c=score+(A[ny][nx]<v?1:0);
                    if(dp[ny][nx]>c){
                        pq.push({c,P(ny,nx)});
                    }
                }
            }
        }

        return dp[N-1][M-1]<=K;

    };

    int ub=1000000000+1,lb=0;//lb以上となる経路は達成できる
    while(ub-lb>1){
        int mid=(ub+lb)/2;
        //cout<<"ub:"<<ub<<",lb:"<<lb<<endl;
        if(check(mid)){
            lb=mid;
        }else{
            ub=mid;
        }
    }

    cout<<lb<<endl;

}
0