結果

問題 No.34 砂漠の行商人
ユーザー sotanishysotanishy
提出日時 2021-08-09 14:02:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,640 ms / 5,000 ms
コード長 1,329 bytes
コンパイル時間 2,450 ms
コンパイル使用メモリ 219,896 KB
実行使用メモリ 75,908 KB
最終ジャッジ日時 2023-10-21 11:46:13
合計ジャッジ時間 35,225 ms
ジャッジサーバーID
(参考情報)
judge11 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 8 ms
4,348 KB
testcase_04 AC 37 ms
5,092 KB
testcase_05 AC 38 ms
5,456 KB
testcase_06 AC 5 ms
4,348 KB
testcase_07 AC 59 ms
6,428 KB
testcase_08 AC 82 ms
8,324 KB
testcase_09 AC 1,860 ms
35,264 KB
testcase_10 AC 4,585 ms
73,996 KB
testcase_11 AC 2,460 ms
75,908 KB
testcase_12 AC 39 ms
5,148 KB
testcase_13 AC 4,640 ms
75,908 KB
testcase_14 AC 4,276 ms
71,760 KB
testcase_15 AC 603 ms
14,972 KB
testcase_16 AC 558 ms
13,780 KB
testcase_17 AC 1,748 ms
31,784 KB
testcase_18 AC 47 ms
4,560 KB
testcase_19 AC 996 ms
25,156 KB
testcase_20 AC 2,008 ms
40,260 KB
testcase_21 AC 6 ms
5,356 KB
testcase_22 AC 2,269 ms
40,772 KB
testcase_23 AC 1,048 ms
21,944 KB
testcase_24 AC 3,991 ms
65,916 KB
testcase_25 AC 146 ms
9,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int dx[4] = {0, 1, 0, -1};
    int dy[4] = {1, 0, -1, 0};

    int N, V, sx, sy, gx, gy;
    cin >> N >> V >> sx >> sy >> gx >> gy;
    --sx, --sy, --gx, --gy;
    V = min(V, 18*(N-1));
    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];
        }
    }
    vector dist(N, vector<vector<int>>(N, vector<int>(V, 1e9)));
    using T = tuple<int, int, int, int>;
    priority_queue<T, vector<T>, greater<T>> pq;
    dist[sx][sy][0] = 0;
    pq.push({0, sx, sy, 0});
    while (!pq.empty()) {
        int d, x, y, t;
        tie(d, x, y, t) = pq.top();
        pq.pop();
        if (dist[x][y][t] < d) continue;
        for (int k = 0; k < 4; ++k) {
            int nx = x + dx[k], ny = y + dy[k];
            if (0 <= nx && nx < N && 0 <= ny && ny < N) {
                int nt = t + L[ny][nx];
                if (nt < V && dist[nx][ny][nt] > d + 1) {
                    dist[nx][ny][nt] = d + 1;
                    pq.push({d + 1, nx, ny, nt});
                }
            }
        }
    }
    int ans = *min_element(dist[gx][gy].begin(), dist[gx][gy].end());
    cout << (ans < 1e9 ? ans : -1) << endl;
}
0