結果

問題 No.20 砂漠のオアシス
ユーザー nanophoto12nanophoto12
提出日時 2016-02-14 23:46:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 2,825 bytes
コンパイル時間 841 ms
コンパイル使用メモリ 96,228 KB
実行使用メモリ 9,032 KB
最終ジャッジ日時 2024-04-21 06:49:17
合計ジャッジ時間 1,753 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,132 KB
testcase_01 AC 4 ms
7,104 KB
testcase_02 AC 4 ms
7,136 KB
testcase_03 AC 5 ms
7,240 KB
testcase_04 AC 5 ms
7,236 KB
testcase_05 AC 20 ms
9,028 KB
testcase_06 AC 17 ms
9,032 KB
testcase_07 AC 26 ms
9,028 KB
testcase_08 AC 19 ms
9,024 KB
testcase_09 AC 25 ms
9,032 KB
testcase_10 AC 4 ms
6,984 KB
testcase_11 AC 4 ms
7,040 KB
testcase_12 AC 4 ms
7,112 KB
testcase_13 AC 5 ms
7,168 KB
testcase_14 AC 7 ms
7,236 KB
testcase_15 AC 5 ms
7,364 KB
testcase_16 AC 8 ms
7,620 KB
testcase_17 AC 6 ms
7,364 KB
testcase_18 AC 7 ms
7,488 KB
testcase_19 AC 8 ms
7,516 KB
testcase_20 AC 4 ms
7,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
#include <sstream>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <iostream>
#include <iomanip>
#include <functional>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(int x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,V,Ox,Oy;

int field[201][201];

struct edge
{
    int to;
    int cost;
};

vector<int> G1[50000];
vector<int> G2[50000];

typedef pair<int,int> P;

int INF = 10000000;

static const int MAX_V = 50000;

vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s, int v, const vector<edge> g[])
{
    priority_queue<P, vector<P>> que;
    que.push(P(v, s));
    fill(d, d + MAX_V, -INF);
    d[s] = v;
    que.push(P(v,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[v] - e.cost > 0)
            {
                if(d[e.to] < d[v] - e.cost)
                {
                    d[e.to] = d[v] - e.cost;
                    que.push(P(d[e.to], e.to));
                }
            }
        }
    }
}

void solve() {
    cin>>N>>V>>Ox>>Oy;
    FOR(i, N)
    FOR(j, N)
    {
        cin >> field[i][j];
    }
    FOR(i, N)
    FOR(j, N)
    {
        int index = i * N + j;
        if(i > 0)
        {
            G[index].push_back({(i-1)*N +j, field[i-1][j]});
        }
        if(i < N-1)
        {
            G[index].push_back({(i+1)*N +j, field[i+1][j]});
        }
        if(j > 0)
        {
            G[index].push_back({i*N +j-1, field[i][j-1]});
        }
        if(j < N - 1)
        {
            G[index].push_back({i*N +j+1, field[i][j+1]});
        }
    }
    dijkstra(0, V, G);
    int dest = N * N -1;
    if(d[dest] > 0)
    {
        cout << "YES" << endl;
        return;
    }
    if(Ox == 0 && Oy == 0)
    {
        cout << "NO" << endl;
        return;
    }
    int t = Ox - 1 + (Oy - 1) * N;
    auto life = d[t];
    if(life <= 0)
    {
        cout << "NO" << endl;
        return;
    }
    dijkstra(t, life * 2, G);
    if(d[dest] > 0)
    {
        cout << "YES" << endl;
        return;
    }
    cout << "NO" << endl;
    return;
}


int main()
{
    solve();
    return 0;
}
0