結果

問題 No.20 砂漠のオアシス
ユーザー ktgktg
提出日時 2016-04-29 14:34:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 2,386 bytes
コンパイル時間 751 ms
コンパイル使用メモリ 89,844 KB
実行使用メモリ 29,764 KB
最終ジャッジ日時 2024-04-21 06:52:47
合計ジャッジ時間 1,905 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
27,588 KB
testcase_01 AC 9 ms
27,588 KB
testcase_02 AC 10 ms
27,588 KB
testcase_03 AC 12 ms
26,952 KB
testcase_04 AC 10 ms
26,948 KB
testcase_05 AC 27 ms
29,764 KB
testcase_06 AC 33 ms
29,124 KB
testcase_07 AC 31 ms
29,252 KB
testcase_08 AC 26 ms
29,384 KB
testcase_09 AC 32 ms
29,632 KB
testcase_10 AC 9 ms
27,968 KB
testcase_11 AC 9 ms
27,588 KB
testcase_12 AC 10 ms
27,076 KB
testcase_13 AC 10 ms
27,204 KB
testcase_14 AC 11 ms
28,232 KB
testcase_15 AC 11 ms
27,588 KB
testcase_16 AC 13 ms
28,612 KB
testcase_17 AC 12 ms
28,100 KB
testcase_18 AC 13 ms
27,848 KB
testcase_19 AC 14 ms
28,484 KB
testcase_20 AC 10 ms
27,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <iterator>

//#define pb push_back
#define puts(x) cout << #x << " : " << x << endl;
//#pragma GCC diagnostic ignored "-Wconversion"
//#define REP(i,n) for (int i=0;i<(n);i++)
//#define REPE(i,n) for (int i=0;i<=(n);i++)
//#define init(a,b) memset((a), (b), (sizeof(a)));
//#define PI 3.14159265
//#define EPS (1e-10)
//#define EQ(a,b) (abs((a)-(b)) < EPS)

using namespace std;

typedef long long ll;
//#define int long long

int dxy[] = {0, 1, 0, -1, 0};

typedef pair<int, int> P;

const int MAX_V=1000000;
const int INF = 1000*1000*1000;
int V;
struct edge { int to, cost; };
typedef pair<int, int> P; // firstは最短距離、secondは頂点の番号

vector<edge> G[MAX_V];
int d[MAX_V];
void add_edge(int from, int to, int cost){
    G[from].push_back(edge{to, cost});
}
void dijkstra(int s) {
    priority_queue<P, vector<P>, greater<P> > que;
    fill(d, d + V, INF);
    d[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
        P p = que.top(); que.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
};

signed main() {
    int n, power,ow,oh;
    cin>>n>>power>>ow>>oh;
    ow--,oh--;
    V = n*n;
    for(int h=0;h<n;h++){
        for(int w=0;w<n;w++){
            int cost;cin>>cost;
            int v_num = h * n + w;
            if(h>0) add_edge(v_num-n, v_num, cost);
            if(h<n-1) add_edge(v_num+n, v_num, cost);
            if(w>0) add_edge(v_num-1, v_num, cost);
            if(w<n-1) add_edge(v_num+1, v_num, cost);
        }
    }
    dijkstra(0);
    if(d[V-1]<power){
        cout<<"YES"<<endl;
        return 0;
    }
    if(oh==-1&&ow==-1){
        cout<<"NO"<<endl;
        return 0;
    }
    int ov = oh * n + ow;
    if (d[ov] >= power) {
        cout<<"NO"<<endl;
        return 0;
    }
    power -= d[ov];
    power *= 2;

    dijkstra(ov);
    if(d[V-1]<power){
        cout<<"YES"<<endl;
        return 0;
    }

    cout<<"NO"<<endl;

    return 0;
}
0