結果
問題 | No.34 砂漠の行商人 |
ユーザー |
![]() |
提出日時 | 2023-12-19 07:32:42 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2,444 ms / 5,000 ms |
コード長 | 1,666 bytes |
コンパイル時間 | 2,650 ms |
コンパイル使用メモリ | 170,320 KB |
実行使用メモリ | 146,688 KB |
最終ジャッジ日時 | 2024-09-27 08:39:18 |
合計ジャッジ時間 | 27,037 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#include <iostream> #include <queue> #include <vector> #include <climits> #include <bits/stdc++.h> using namespace std; int main() { int N, V, Sy, Sx, Gy, Gx; cin >> N >> V >> Sy >> Sx >> Gy >> Gx; Sx--; Sy--; Gx--; Gy--; 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]; } } const long long inf = LLONG_MAX; vector<vector<long long>> dist(18 * N, vector<long long>(N * N, inf)); dist[0][Sx * N + Sy] = 0; deque<tuple<long long, int, int, int>> queue; queue.push_back(make_tuple(0, 0, Sx, Sy)); while (!queue.empty()) { tuple<long long, int, int, int> tup = queue.front(); long long d = get<0>(tup); int v = get<1>(tup); int x = get<2>(tup); int y = get<3>(tup); queue.pop_front(); if (dist[v][x * N + y] < d) { continue; } for (auto [dx, dy] : vector<pair<int, int>>{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}) { int xx = x + dx; int yy = y + dy; if (0 <= xx && xx < N && 0 <= yy && yy < N) { int vv = v + L[xx][yy]; if (vv < 18 * N && dist[vv][xx * N + yy] > d + 1) { dist[vv][xx * N + yy] = d + 1; queue.push_back({d + 1, vv, xx, yy}); } } } } long long ans = inf; for (int v = 0; v < 18 * N; ++v) { if (v < V) { ans = min(ans, dist[v][Gx * N + Gy]); } } if (ans == inf) { ans = -1; } cout << ans << endl; return 0; }