結果
問題 |
No.34 砂漠の行商人
|
ユーザー |
|
提出日時 | 2015-08-19 20:10:22 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 144 ms / 5,000 ms |
コード長 | 1,786 bytes |
コンパイル時間 | 1,382 ms |
コンパイル使用メモリ | 167,668 KB |
実行使用メモリ | 83,488 KB |
最終ジャッジ日時 | 2024-06-28 10:20:48 |
合計ジャッジ時間 | 3,510 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> #define rep(i, a) for (int i = 0; i < (a); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define repr(i, a) for (int i = (a) - 1; i >= 0; i--) #define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--) using namespace std; typedef long long ll; const ll inf = 1e9; const ll mod = 1e9 + 7; int dp[100][100][2000]; // y, x, damage => dist typedef tuple<int, int, int, int> P; // dist + h, damage, y, x int N, V, sy, sx, gy, gx, L[100][100]; int main() { cin >> N >> V >> sy >> sx >> gy >> gx; sy--; sx--; gy--; gx--; rep (j, N) rep (i, N) cin >> L[i][j]; if (V >= 2000) { int ans = abs(gy - sy) + abs(gx - sx); cout << ans << endl; return 0; } priority_queue<P, vector<P>, greater<P>> q; q.emplace(abs(sy - gy) + abs(sx - gx), 0, sy, sx); rep (i, 100) rep (j, 100) rep (k, 2000) dp[i][j][k] = inf; dp[sy][sx][0] = 0; while (!q.empty()) { P p = q.top(); q.pop(); int damage = get<1>(p); int y = get<2>(p); int x = get<3>(p); if (y == gy && x == gx) break; int dy[] = {0, 1, 0, -1}; int dx[] = {1, 0, -1, 0}; rep (k, 4) { int ny = y + dy[k]; int nx = x + dx[k]; if (ny < 0 || ny >= N || nx < 0 || nx >= N) continue; if (damage + L[ny][nx] >= V) continue; if (dp[ny][nx][damage + L[ny][nx]] > dp[y][x][damage] + 1) { dp[ny][nx][damage + L[ny][nx]] = dp[y][x][damage] + 1; int h = abs(ny - gy) + abs(nx - gx); q.emplace(dp[ny][nx][damage + L[ny][nx]] + h, damage + L[ny][nx], ny, nx); } } } int ans = inf; rep (k, V) { ans = min(ans, dp[gy][gx][k]); } if (ans == inf) { cout << -1 << endl; } else { cout << ans << endl; } return 0; }