結果
| 問題 | No.34 砂漠の行商人 |
| コンテスト | |
| ユーザー |
sotanishy
|
| 提出日時 | 2021-08-09 14:02:19 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 3,118 ms / 5,000 ms |
| コード長 | 1,329 bytes |
| 記録 | |
| コンパイル時間 | 2,228 ms |
| コンパイル使用メモリ | 229,784 KB |
| 実行使用メモリ | 76,172 KB |
| 最終ジャッジ日時 | 2026-06-22 12:54:16 |
| 合計ジャッジ時間 | 26,185 ms |
|
ジャッジサーバーID (参考情報) |
judge2_1 / judge3_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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;
}
sotanishy