結果
問題 | No.34 砂漠の行商人 |
ユーザー |
![]() |
提出日時 | 2020-09-05 21:16:17 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,040 ms / 5,000 ms |
コード長 | 1,766 bytes |
コンパイル時間 | 1,950 ms |
コンパイル使用メモリ | 179,840 KB |
実行使用メモリ | 77,164 KB |
最終ジャッジ日時 | 2024-11-29 06:08:46 |
合計ジャッジ時間 | 4,517 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) #define rrep(i,n) for (int i = (n)-1; i>=0; --i) #define chmax(a, b) a = max(a, b) #define chmin(a, b) a = min(a, b) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; using P = pair<int,int>; using VI = vector<int>; using VVI = vector<VI>; int dist[100][100][2000]; int l[100][100]; const int INF = 10000; const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; struct cmp { bool operator()(tuple<int,int,int,int> &x, tuple<int,int,int,int> &y) { return get<0>(x) > get<0>(y); } }; int main() { int n, v, sx, sy, gx, gy; scanf("%d%d%d%d%d%d", &n, &v, &sy, &sx, &gy, &gx); --sx; --sy; --gx; --gy; rep(i, n) rep(j, n) scanf("%d", &l[i][j]); int shortest_dist = abs(sx - gx) + abs(sy - gy); if (v > shortest_dist * 9) { printf("%d\n", shortest_dist); return 0; } rrep(i, n)rrep(j, n)rrep(k, v + 1) dist[i][j][k] = INF; priority_queue<tuple<int,int,int,int>,vector<tuple<int,int,int,int>>,cmp> q; dist[sx][sy][v] = 0; q.emplace(0, sx, sy, v); while(!q.empty()) { int d, x, y, vit; tie(d, x, y, vit) = q.top(); q.pop(); if (d > dist[x][y][vit]) continue; rrep(k, 4) { int nx = x + dx[k], ny = y + dy[k]; if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue; int nvit = vit - l[nx][ny]; int nd = d + 1; if (nvit > 0 && nd < dist[nx][ny][nvit]) { dist[nx][ny][nvit] = nd; q.emplace(nd, nx, ny, nvit); } } } int ans = INF; rrep(k, v + 1) chmin(ans, dist[gx][gy][k]); if (ans == INF) ans = -1; printf("%d\n", ans); }