結果

問題 No.971 いたずらっ子
ユーザー kya_skikya_ski
提出日時 2020-01-18 13:09:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,368 ms / 2,000 ms
コード長 2,259 bytes
コンパイル時間 2,155 ms
コンパイル使用メモリ 179,220 KB
実行使用メモリ 324,352 KB
最終ジャッジ日時 2024-04-25 02:07:55
合計ジャッジ時間 14,594 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,362 ms
324,224 KB
testcase_01 AC 1,368 ms
324,352 KB
testcase_02 AC 980 ms
242,048 KB
testcase_03 AC 1,228 ms
323,968 KB
testcase_04 AC 1,145 ms
305,152 KB
testcase_05 AC 1,218 ms
323,840 KB
testcase_06 AC 912 ms
249,088 KB
testcase_07 AC 10 ms
5,888 KB
testcase_08 AC 283 ms
87,296 KB
testcase_09 AC 265 ms
83,584 KB
testcase_10 AC 8 ms
6,016 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int dh[2] = {0, 1}, dw[2] = {1, 0};
vector<long long> dijkstra(const vector<vector<pair<int, long long>>> &G, int s){
    struct RadixHeap {
        using P = pair<long long, int>;
        int bsr(long long x){ return (x == 0) ? -1 : 63-__builtin_clz(x); }
        vector<P> v[65];
        long long last, sz;
        RadixHeap() : last(0), sz(0) { }
        bool empty () { return (sz == 0); }
        void push(long long x, int u) { sz++; v[bsr(x^last)+1].emplace_back(x, u); }
        P pop () {
            if (!v[0].size()) {
                int i = 1;
                while (!v[i].size()) { i++; }
                last = min_element(v[i].begin(), v[i].end())->first;
                for (auto x : v[i]) { v[bsr(x.first^last)+1].emplace_back(x); }
                v[i].clear();
            }
            P res = v[0].back(); sz--; v[0].pop_back(); return res;
        }
    };
    const long long inf = INT64_MAX;
    vector<long long> ret(G.size(), inf); ret[s] = 0;
    RadixHeap rh; rh.push(ret[s], s);
    while (!rh.empty()) {
        long long dist; int node;
        tie(dist, node) = rh.pop();
        if (ret[node] < dist) continue;
        for (auto &e : G[node]) {
            int next_node; long long next_dist;
            tie(next_node, next_dist) = e;
            if (ret[next_node] > ret[node] + next_dist){
                ret[next_node] = ret[node] + next_dist;
                rh.push(ret[next_node], next_node);
            }
        }
    }
    return ret;
}

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    for (int i = 0; i < H; i++) cin >> S[i];
    
    auto idx = [&](int h, int w) { return h*W+w; };
    vector<vector<pair<int, long long>>> G(H*W);
    for (int h = 0; h < H; h++) for (int w = 0; w < W; w++) {
        int from = idx(h, w);
        for (int i = 0; i < 2; i++) {
            int nh = h + dh[i], nw = w + dw[i], to = idx(nh, nw);
            if (nh < 0 || H <= nh || nw < 0 || W <= nw) continue;
            if (S[nh][nw] == 'k') G[from].emplace_back(to, nh+nw+1);
            else G[from].emplace_back(to, 1);
        }
    }
    
    vector<long long> dist = dijkstra(G, 0);
    cout << dist[idx(H-1, W-1)] << endl;
    return 0;
}
0