結果

問題 No.34 砂漠の行商人
ユーザー KudeKude
提出日時 2020-09-05 21:16:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,018 ms / 5,000 ms
コード長 1,766 bytes
コンパイル時間 1,648 ms
コンパイル使用メモリ 175,660 KB
実行使用メモリ 82,948 KB
最終ジャッジ日時 2023-08-19 13:04:55
合計ジャッジ時間 4,700 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
5,644 KB
testcase_02 AC 5 ms
18,092 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 30 ms
41,140 KB
testcase_05 AC 31 ms
47,504 KB
testcase_06 AC 10 ms
36,852 KB
testcase_07 AC 46 ms
57,940 KB
testcase_08 AC 61 ms
68,260 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 31 ms
43,268 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 578 ms
80,284 KB
testcase_20 AC 1,018 ms
82,948 KB
testcase_21 AC 20 ms
80,460 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 91 ms
60,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,n) for (int i = (n)-1; i>=0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;

int dist[100][100][2000];
int l[100][100];
const int INF = 10000;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

struct cmp {
    bool operator()(tuple<int,int,int,int> &x, tuple<int,int,int,int> &y) {
        return get<0>(x) > get<0>(y);
    }
};

int main() {
    int n, v, sx, sy, gx, gy;
    scanf("%d%d%d%d%d%d", &n, &v, &sy, &sx, &gy, &gx);
    --sx; --sy; --gx; --gy;
    rep(i, n) rep(j, n) scanf("%d", &l[i][j]);
    int shortest_dist = abs(sx - gx) + abs(sy - gy);
    if (v > shortest_dist * 9) {
        printf("%d\n", shortest_dist);
        return 0;
    }
    rrep(i, n)rrep(j, n)rrep(k, v + 1) dist[i][j][k] = INF;
    priority_queue<tuple<int,int,int,int>,vector<tuple<int,int,int,int>>,cmp> q;
    dist[sx][sy][v] = 0;
    q.emplace(0, sx, sy, v);
    while(!q.empty()) {
        int d, x, y, vit;
        tie(d, x, y, vit) = q.top(); q.pop();
        if (d > dist[x][y][vit]) continue;
        rrep(k, 4) {
            int nx = x + dx[k], ny = y + dy[k];
            if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
            int nvit = vit - l[nx][ny];
            int nd = d + 1;
            if (nvit > 0 && nd < dist[nx][ny][nvit]) {
                dist[nx][ny][nvit] = nd;
                q.emplace(nd, nx, ny, nvit);
            }
        }
    }
    int ans = INF;
    rrep(k, v + 1) chmin(ans, dist[gx][gy][k]);
    if (ans == INF) ans = -1;
    printf("%d\n", ans);
}
0