結果
問題 | No.34 砂漠の行商人 |
ユーザー |
![]() |
提出日時 | 2021-08-09 14:02:19 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 4,837 ms / 5,000 ms |
コード長 | 1,329 bytes |
コンパイル時間 | 2,473 ms |
コンパイル使用メモリ | 209,812 KB |
最終ジャッジ日時 | 2025-01-23 17:11:28 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int N, V, sx, sy, gx, gy; cin >> N >> V >> sx >> sy >> gx >> gy; --sx, --sy, --gx, --gy; V = min(V, 18*(N-1)); vector<vector<int>> L(N, vector<int>(N)); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { cin >> L[i][j]; } } vector dist(N, vector<vector<int>>(N, vector<int>(V, 1e9))); using T = tuple<int, int, int, int>; priority_queue<T, vector<T>, greater<T>> pq; dist[sx][sy][0] = 0; pq.push({0, sx, sy, 0}); while (!pq.empty()) { int d, x, y, t; tie(d, x, y, t) = pq.top(); pq.pop(); if (dist[x][y][t] < d) continue; for (int k = 0; k < 4; ++k) { int nx = x + dx[k], ny = y + dy[k]; if (0 <= nx && nx < N && 0 <= ny && ny < N) { int nt = t + L[ny][nx]; if (nt < V && dist[nx][ny][nt] > d + 1) { dist[nx][ny][nt] = d + 1; pq.push({d + 1, nx, ny, nt}); } } } } int ans = *min_element(dist[gx][gy].begin(), dist[gx][gy].end()); cout << (ans < 1e9 ? ans : -1) << endl; }