結果

問題 No.34 砂漠の行商人
ユーザー 👑 rin204rin204
提出日時 2022-07-10 17:17:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 874 ms / 5,000 ms
コード長 1,488 bytes
コンパイル時間 2,603 ms
コンパイル使用メモリ 205,988 KB
実行使用メモリ 394,196 KB
最終ジャッジ日時 2023-09-02 00:17:13
合計ジャッジ時間 7,247 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 11 ms
4,384 KB
testcase_05 AC 11 ms
5,076 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 18 ms
5,692 KB
testcase_08 AC 24 ms
7,464 KB
testcase_09 AC 309 ms
228,884 KB
testcase_10 AC 152 ms
393,532 KB
testcase_11 AC 675 ms
394,196 KB
testcase_12 AC 9 ms
4,380 KB
testcase_13 AC 874 ms
100,912 KB
testcase_14 AC 629 ms
97,132 KB
testcase_15 AC 14 ms
42,384 KB
testcase_16 AC 57 ms
80,724 KB
testcase_17 AC 8 ms
30,072 KB
testcase_18 AC 6 ms
8,856 KB
testcase_19 AC 173 ms
22,840 KB
testcase_20 AC 325 ms
37,800 KB
testcase_21 AC 4 ms
4,840 KB
testcase_22 AC 15 ms
37,800 KB
testcase_23 AC 30 ms
115,020 KB
testcase_24 AC 477 ms
205,372 KB
testcase_25 AC 25 ms
8,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

void solve(){
    int n, v, sy, sx, gy, gx;
    cin >> n >> v >> sy >> sx >> gy >> gx;
    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];
    sx--; sy--; gx--; gy--;
    auto f = [&](int i, int j, int k) -> int{
        return (i * n + j) * v + k;
    };
    vector<int> dist(n * n * v, -1);
    dist[f(sx, sy, v - 1)] = 0;
    queue<int> que;
    que.push(f(sx, sy, v - 1));
    while(!que.empty()){
        int tmp = que.front();
        que.pop();
        int x = tmp / (n * v);
        tmp -= x * n * v;
        int y = tmp / v;
        int vv = tmp - y * v;
        for(int t = 0; t < 4; t++){
            int nx = x + dx[t];
            int ny = y + dy[t];
            if(nx == -1 || nx == n || ny == -1 || ny == n) continue;
            int nv = vv - L[nx][ny];
            if(nv < 0) continue;
            if(dist[f(nx, ny, nv)] == -1){
                if(nx == gx && ny == gy){
                    cout << dist[f(x, y, vv)] + 1 << "\n";
                    return;
                }
                dist[f(nx, ny, nv)] = dist[f(x, y, vv)] + 1;
                que.push(f(nx, ny, nv));
            }
        }
    }
    cout << "-1\n";
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t;
    t = 1;
    //cin >> t;
    while(t--) solve();
}
0