結果

問題 No.20 砂漠のオアシス
ユーザー koba-e964koba-e964
提出日時 2016-12-26 14:17:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 54 ms / 5,000 ms
コード長 2,877 bytes
コンパイル時間 836 ms
コンパイル使用メモリ 98,784 KB
実行使用メモリ 6,560 KB
最終ジャッジ日時 2023-08-03 08:08:31
合計ジャッジ時間 2,045 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 45 ms
6,052 KB
testcase_06 AC 53 ms
6,560 KB
testcase_07 AC 54 ms
6,468 KB
testcase_08 AC 33 ms
6,228 KB
testcase_09 AC 54 ms
6,560 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 11 ms
4,380 KB
testcase_17 AC 8 ms
4,380 KB
testcase_18 AC 10 ms
4,376 KB
testcase_19 AC 11 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

/**
 * Dijkstra's algorithm.
 * First, call add_edge() to add edges.
 * Second, call solve() to calculate the length of the shortest path from source to each vertex.
 * Header requirement: algorithm, queue, vector
 * Verified by AtCoder ARC026-C (http://arc026.contest.atcoder.jp/submissions/604231)
 */
 template<class Len = int>
class Dijkstra {
private:
  int n;
  std::vector<std::vector<std::pair<int, Len> > > edges;
public:
  /**
   * n: the number of vertices
   */
  Dijkstra(int n) : n(n), edges(n) {}
  /*
   * from: the source of edge to add
   * to: the target of edge to add
   * cost: the cost of edge to add
   */
  void add_edge(int from, int to, Len cost) {
    edges[from].push_back(std::pair<int, Len>(to, cost));
  }
  /*
   * This function returns an array consisting of the distances from vertex source.
   */
  std::vector<Len> solve(int source) {
    const Len inf = 1e8;
    typedef std::pair<Len, int> pi;
    std::vector<Len> d(n, inf);
    std::priority_queue<pi, std::vector<pi>, std::greater<pi> > que;
    que.push(pi(0, source));
    while (!que.empty()) {
      pi p = que.top(); que.pop();
      int idx = p.second;
      if (d[idx] <= p.first) {
	continue;
      }
      d[idx] = p.first;
      for(int j = 0; j < edges[idx].size(); ++j) {
	que.push(pi(p.first + edges[idx][j].second, edges[idx][j].first));
      }
    }
    return d;
  }
};


using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;



int main(void){
  int n, v, ox, oy;
  cin >> n >> v >> ox >> oy;
  vector<VI> l(n, VI(n));
  REP(i, 0, n) {
    REP(j, 0, n) {
      cin >> l[i][j];
    }
  }
  // Constructs a graph
  Dijkstra<int> dijk(n * n);
  REP(i, 0, n) {
    REP(j, 0, n) {
      int dx[4] = {1, 0, -1, 0};
      int dy[4] = {0, 1, 0, -1};
      REP(d, 0, 4) {
	int tx = i + dx[d];
	int ty = j + dy[d];
	if (0 > tx || tx >= n || 0 > ty || ty >= n) {
	  continue;
	}
	dijk.add_edge(tx * n + ty, i * n + j, l[i][j]);
      }
    }
  }
  VI sol = dijk.solve(0);
  bool reachable = false;
  if (sol[n * n - 1] < v) {
    reachable = true;
  }
  if (not reachable && ox >= 1) {
    ox--, oy--;
    int to_oasis = sol[oy * n + ox];
    if (to_oasis < v) {
      int rem = 2 * (v - to_oasis);
      VI sol2 = dijk.solve(oy * n + ox);
      reachable = sol2[n * n - 1] < rem;
    }
  }
  cout << (reachable ? "YES": "NO") << endl;
}
0