結果

問題 No.34 砂漠の行商人
ユーザー 👑 rin204rin204
提出日時 2022-07-10 17:16:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,486 bytes
コンパイル時間 2,448 ms
コンパイル使用メモリ 211,172 KB
実行使用メモリ 391,332 KB
最終ジャッジ日時 2023-09-02 00:14:52
合計ジャッジ時間 16,225 ms
ジャッジサーバーID
(参考情報)
judge15 / judge16
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 44 ms
9,556 KB
testcase_05 AC 45 ms
9,656 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 80 ms
14,608 KB
testcase_08 AC 129 ms
22,384 KB
testcase_09 AC 1,959 ms
181,052 KB
testcase_10 AC 107 ms
22,416 KB
testcase_11 AC 3,694 ms
391,332 KB
testcase_12 AC 31 ms
7,888 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
    };
    unordered_map<int, int> dist;
    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.count(f(nx, ny, nv))){
                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