結果

問題 No.34 砂漠の行商人
ユーザー ninoinuininoinui
提出日時 2020-07-25 07:05:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,161 bytes
コンパイル時間 2,534 ms
コンパイル使用メモリ 215,512 KB
実行使用メモリ 84,468 KB
最終ジャッジ日時 2023-09-08 16:58:19
合計ジャッジ時間 13,098 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 9 ms
4,380 KB
testcase_04 AC 43 ms
4,856 KB
testcase_05 AC 42 ms
5,280 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 67 ms
6,336 KB
testcase_08 AC 94 ms
8,248 KB
testcase_09 AC 3,212 ms
49,988 KB
testcase_10 TLE -
testcase_11 AC 3,643 ms
84,468 KB
testcase_12 AC 44 ms
4,892 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 1,558 ms
26,940 KB
testcase_16 AC 1,446 ms
25,536 KB
testcase_17 AC 1,907 ms
31,664 KB
testcase_18 AC 275 ms
8,216 KB
testcase_19 AC 1,076 ms
24,868 KB
testcase_20 AC 2,220 ms
40,104 KB
testcase_21 AC 7 ms
5,596 KB
testcase_22 AC 2,479 ms
40,540 KB
testcase_23 AC 2,141 ms
35,748 KB
testcase_24 TLE -
testcase_25 AC 159 ms
9,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, V, SX, SY, GX, GY;
  cin >> N >> V >> SX >> SY >> GX >> GY;
  V = min(2000, V), SX--, SY--, GX--, GY--;
  vector L(N, vector(N, 0));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) cin >> L.at(i).at(j);
  }
  
  vector dist(N, vector(N, vector(V + 1, INT_MAX)));
  dist.at(SY).at(SX).at(V) = 0;
  vector mx {-1, 0, 1, 0};
  vector my {0, 1, 0, -1};

  using t4 = tuple<int, int, int, int>;
  priority_queue<t4, vector<t4>, greater<t4>> PQ;
  PQ.push({0, SY, SX, V});

  while (PQ.size()) {
    auto [d, y, x, v] = PQ.top();
    PQ.pop();
    for (int i = 0; i < 4; i++) {
      int ny = y + my.at(i);
      int nx = x + mx.at(i);
      if (ny < 0 || ny >= N || nx < 0 || nx >= N) continue;
      int nv = v - L.at(ny).at(nx);
      if (nv <= 0) continue;
      int nd = d + 1;
      if (nd > dist.at(ny).at(nx).at(nv)) continue;
      if (nd < dist.at(ny).at(nx).at(nv)) PQ.push({nd, ny, nx, nv});
      dist.at(ny).at(nx).at(nv) = nd;
    }
  }
  int ans = *min_element(dist.at(GY).at(GX).begin(), dist.at(GY).at(GX).end());
  cout << ((ans == INT_MAX) ? -1 : ans) << "\n";
}
0