結果

問題 No.34 砂漠の行商人
ユーザー ninoinuininoinui
提出日時 2020-07-25 06:51:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,170 bytes
コンパイル時間 2,353 ms
コンパイル使用メモリ 215,260 KB
実行使用メモリ 533,304 KB
最終ジャッジ日時 2023-09-08 16:31:45
合計ジャッジ時間 9,444 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,756 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 50 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 MLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
  int N, V, SX, SY, GX, GY;
  cin >> N >> V >> SX >> SY >> GX >> GY;
  SX--, SY--, GX--, GY--;
  V = min(2000, V);
  vector<vector<int>> L(N, vector<int>(N));
  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();
    if (d > dist.at(y).at(x).at(v)) continue;
    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 (ny == GY && nx == GX) return cout << nd << "\n", 0;
      dist.at(ny).at(nx).at(nv) = dist.at(y).at(x).at(v) + 1;
      PQ.push({nd, ny, nx, nv});
    }
  }
  cout << -1 << "\n";
}
0