結果

問題 No.34 砂漠の行商人
ユーザー pekempeypekempey
提出日時 2015-08-19 20:06:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 762 ms / 5,000 ms
コード長 1,675 bytes
コンパイル時間 1,192 ms
コンパイル使用メモリ 155,204 KB
実行使用メモリ 426,704 KB
最終ジャッジ日時 2023-09-10 19:11:43
合計ジャッジ時間 8,324 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 191 ms
393,968 KB
testcase_01 AC 189 ms
394,016 KB
testcase_02 AC 191 ms
394,048 KB
testcase_03 AC 189 ms
394,028 KB
testcase_04 AC 222 ms
394,120 KB
testcase_05 AC 180 ms
394,284 KB
testcase_06 AC 169 ms
394,100 KB
testcase_07 AC 241 ms
394,292 KB
testcase_08 AC 227 ms
395,792 KB
testcase_09 AC 244 ms
394,736 KB
testcase_10 AC 170 ms
394,072 KB
testcase_11 AC 350 ms
397,960 KB
testcase_12 AC 169 ms
394,188 KB
testcase_13 AC 762 ms
426,704 KB
testcase_14 AC 370 ms
395,736 KB
testcase_15 AC 169 ms
393,984 KB
testcase_16 AC 174 ms
394,140 KB
testcase_17 AC 169 ms
393,976 KB
testcase_18 AC 171 ms
394,124 KB
testcase_19 AC 187 ms
394,356 KB
testcase_20 AC 212 ms
394,368 KB
testcase_21 AC 172 ms
394,040 KB
testcase_22 AC 171 ms
394,212 KB
testcase_23 AC 169 ms
394,072 KB
testcase_24 AC 280 ms
394,692 KB
testcase_25 AC 170 ms
394,156 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;

int dp[100][100][10000]; // 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];

    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, 10000) 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;
}
0