結果

問題 No.34 砂漠の行商人
コンテスト
ユーザー himueu
提出日時 2026-10-21 17:17:38
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc16.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 556 ms / 5,000 ms
+ 257µs
コード長 1,263 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,968 ms
コンパイル使用メモリ 229,800 KB
実行使用メモリ 75,648 KB
最終ジャッジ日時 2026-10-21 17:17:45
合計ジャッジ時間 6,761 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge5_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>

int main() {
    using namespace std;
    int n, v, sx, sy, gx, gy;
    cin >> n >> v >> sx >> sy >> gx >> gy;
    sx--; sy--; gx--; gy--;
    v = min(v, n * 18);
    vector L(n, vector<int>(n));
    for (auto &l : L) {
        for (auto &e : l) { cin >> e; }
    }

    constexpr int INF = 1e9;
    vector dist(n, vector(n, vector<int>(v + 1, INF)));
    dist[sy][sx][v] = 0;
    queue<tuple<int, int, int>> que;
    que.emplace(sy, sx, v);
    constexpr int dy[] = {0, -1, 0, 1};
    constexpr int dx[] = {-1, 0, 1, 0};
    while (!que.empty()) {
        auto [y, x, l] = que.front();
        if (y == gy && x == gx) {
            cout << dist[y][x][l] << endl;
            return 0;
        }
        que.pop();
        for (int i = 0; i < 4; i++) {
            int ny = y + dy[i], nx = x + dx[i];
            if (ny < 0 || ny >= n || nx < 0 || nx >= n) { continue; }
            int nl = l - L[ny][nx];
            if (nl <= 0) { continue; }
            if (dist[ny][nx][nl] > dist[y][x][l] + 1) {
                dist[ny][nx][nl] = dist[y][x][l] + 1;
                que.emplace(ny, nx, nl);
            }
        }
    }
    cout << -1 << endl;
}
0