結果

問題 No.34 砂漠の行商人
ユーザー pekempeypekempey
提出日時 2015-08-19 19:56:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 3,682 ms / 5,000 ms
コード長 1,679 bytes
コンパイル時間 1,382 ms
コンパイル使用メモリ 156,320 KB
実行使用メモリ 200,628 KB
最終ジャッジ日時 2023-09-10 19:12:39
合計ジャッジ時間 20,064 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
198,820 KB
testcase_01 AC 117 ms
199,020 KB
testcase_02 AC 119 ms
198,732 KB
testcase_03 AC 117 ms
198,692 KB
testcase_04 AC 153 ms
198,828 KB
testcase_05 AC 153 ms
198,840 KB
testcase_06 AC 119 ms
198,840 KB
testcase_07 AC 175 ms
198,832 KB
testcase_08 AC 198 ms
198,852 KB
testcase_09 AC 1,160 ms
199,388 KB
testcase_10 AC 189 ms
198,968 KB
testcase_11 AC 2,473 ms
200,628 KB
testcase_12 AC 141 ms
198,968 KB
testcase_13 AC 3,682 ms
200,436 KB
testcase_14 AC 2,695 ms
200,616 KB
testcase_15 AC 123 ms
198,900 KB
testcase_16 AC 264 ms
199,160 KB
testcase_17 AC 118 ms
198,940 KB
testcase_18 AC 130 ms
198,840 KB
testcase_19 AC 892 ms
200,484 KB
testcase_20 AC 1,498 ms
200,628 KB
testcase_21 AC 121 ms
198,900 KB
testcase_22 AC 134 ms
198,916 KB
testcase_23 AC 121 ms
198,904 KB
testcase_24 AC 2,005 ms
200,492 KB
testcase_25 AC 208 ms
199,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;

short dp[100][100][10000]; // y, x, damage => dist
typedef tuple<int, int, int, int> P; // dist, 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];

    priority_queue<P, vector<P>, greater<P>> q;
    q.emplace(0, 0, sy, sx);

    rep (i, 100) rep (j, 100) rep (k, 10000) dp[i][j][k] = 10101;
    dp[sy][sx][0] = 0;

    while (!q.empty()) {
    	P p = q.top(); q.pop();
    	int dist = get<0>(p);
    	int damage = get<1>(p);
    	int y = get<2>(p);	
    	int x = get<3>(p);

    	if (y == gy && x == gx) break;
    	if (dp[y][x][damage] < dist) continue;

    	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;
    			q.emplace(dp[ny][nx][damage + L[ny][nx]], damage + L[ny][nx], ny, nx);
    		}
    	}
    }

    int ans = inf;

    rep (k, V) {
    	ans = min(ans, (int)dp[gy][gx][k]);
    }

    if (ans == 10101) {
    	cout << -1 << endl;
    } else {
    	cout << ans << endl;
    }

    return 0;
}
0