結果
| 問題 | No.20 砂漠のオアシス | 
| コンテスト | |
| ユーザー |  srjywrdnprkt | 
| 提出日時 | 2022-12-19 12:29:02 | 
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 18 ms / 5,000 ms | 
| コード長 | 2,371 bytes | 
| コンパイル時間 | 1,123 ms | 
| コンパイル使用メモリ | 141,536 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-11-18 00:52:54 | 
| 合計ジャッジ時間 | 2,103 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 21 | 
ソースコード
#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
 
using namespace std;
template<typename T> using pq = priority_queue<T, vector<T>, greater<T>>;
 
int main(){
 
    bool ok=0;
    int N, V, h, w, oh, ow, alt, d, nh, nw;
    cin >> N >> V >> oh >> ow;
    oh--; ow--;
    swap(oh, ow);
    
    pq<tuple<int, int, int>> que;
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    vector<vector<int>> dist(N, vector<int>(N, 1e9));
    vector<vector<bool>> visit(N, vector<bool>(N));
    vector<vector<int>> field(N, vector<int>(N));
 
    for (int i=0; i < N; i++){
        for (int j=0; j<N; j++) cin >> field[i][j];
    }
 
    dist[0][0] = 0;
    que.push({0, 0, 0});
 
    while(!que.empty()){
        tie(d, h, w) = que.top();
        que.pop();
        if (visit[h][w]) continue;
        visit[h][w] = 1;
        for (int dir=0; dir < 4; dir++){
            nh = h + dx[dir];
            nw = w + dy[dir];
            if (nh < 0 || nh >= N || nw < 0 || nw >= N) continue;
            alt = dist[h][w] + field[nh][nw];
            if (alt < dist[nh][nw]){
                dist[nh][nw] = alt;
                que.push({dist[nh][nw], nh, nw});
            }
        }
    }
    if (dist[N-1][N-1] < V) ok = 1;
    if (oh >= 0 && oh < N && ow >= 0 && ow < N) V -= dist[oh][ow];
    if (V < 0){ 
        cout << "NO" << endl;
        return 0;
    }   
    V *= 2;
    for (int i=0; i<N; i++){
        for (int j=0; j<N; j++){
            dist[i][j] = 1e9; visit[i][j] = 0;
        }
    }
    if (oh >= 0 && oh < N && ow >= 0 && ow < N){
        dist[oh][ow] = 0;
        que.push({field[oh][ow], oh, ow});
 
        while(!que.empty()){
            tie(d, h, w) = que.top();
            que.pop();
            if (visit[h][w]) continue;
            visit[h][w] = 1;
            for (int dir=0; dir < 4; dir++){
                nh = h + dx[dir];
                nw = w + dy[dir];
                if (nh < 0 || nh >= N || nw < 0 || nw >= N) continue;
                alt = dist[h][w] + field[nh][nw];
                if (alt < dist[nh][nw]){
                    dist[nh][nw] = alt;
                    que.push({dist[nh][nw], nh, nw});
                }
            }
        }
        if (dist[N-1][N-1] < V) ok = 1;
    }
    cout << (ok ? "YES" : "NO") << endl;
 
    return 0;
}
            
            
            
        