結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  ninoinui | 
| 提出日時 | 2020-07-25 07:08:07 | 
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) | 
| 結果 | 
                                TLE
                                 
                            (最新) 
                                AC
                                 
                            (最初) | 
| 実行時間 | - | 
| コード長 | 1,223 bytes | 
| コンパイル時間 | 11,787 ms | 
| コンパイル使用メモリ | 273,596 KB | 
| 最終ジャッジ日時 | 2025-01-12 05:19:09 | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 24 TLE * 2 | 
ソースコード
#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 (ny == GY && nx == GX) return cout << nd << "\n", 0;
      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";
}
            
            
            
        