結果

問題 No.971 いたずらっ子
ユーザー 👑 hitonanodehitonanode
提出日時 2020-01-18 00:08:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 835 ms / 2,000 ms
コード長 2,065 bytes
コンパイル時間 1,959 ms
コンパイル使用メモリ 97,540 KB
実行使用メモリ 73,584 KB
最終ジャッジ日時 2023-08-07 07:59:05
合計ジャッジ時間 8,697 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 835 ms
73,584 KB
testcase_01 AC 829 ms
73,532 KB
testcase_02 AC 594 ms
54,108 KB
testcase_03 AC 697 ms
73,368 KB
testcase_04 AC 654 ms
69,348 KB
testcase_05 AC 705 ms
73,264 KB
testcase_06 AC 506 ms
55,064 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 162 ms
21,624 KB
testcase_09 AC 155 ms
20,968 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,388 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>

int H, W;
std::vector<std::string> A;

using lint = long long int;
lint get_d(int x, int y) {
    lint dd = 1 + (A[x][y] == 'k') * (x + y);
    return dd;
}

#include <array>
#include <queue>
#include <tuple>
#include <utility>
#include <vector>
struct GridGraph
{
    using T_E = lint;
    const T_E INF = 1e18;
    int H, W;
    std::array<int, 2> dx = {1, 0};
    std::array<int, 2> dy = {0, 1};

    GridGraph() = default;
    GridGraph(int h, int w) : H(h), W(w) {}

    inline T_E edge_cost(int x_s, int y_s, int x_t, int y_t) {
        return get_d(x_t, y_t);
    }

    // Dijkstra's algorithm
    // Complexity: O(HWlog(HW))
    std::vector<std::vector<T_E>> dij; // Distance from (x_s, y_s)
    std::vector<std::vector<std::pair<int, int>>> dij_prv; // Previous node for Dijkstra optimal path
    void dijkstra(int x_s, int y_s) {
        dij.assign(H, std::vector<T_E>(W, INF));
        dij_prv.assign(H, std::vector<std::pair<int, int>>(W, std::make_pair(-1, -1)));

        using P = std::tuple<T_E, int, int>;
        std::priority_queue<P, std::vector<P>, std::greater<P>> pq;
        pq.emplace(0, x_s, y_s);
        while (!pq.empty()) {
            T_E dnow;
            int x, y;
            std::tie(dnow, x, y) = pq.top();
            pq.pop();
            if (dij[x][y] < dnow) continue;
            for (size_t d = 0; d < dx.size(); d++) {
                int xn = x + dx[d];
                int yn = y + dy[d];
                if (xn < 0 or yn < 0 or xn >= H or yn >= W) continue;
                T_E dnxt = dnow + edge_cost(x, y, xn, yn);
                if (dij[xn][yn] > dnxt) {
                    dij[xn][yn] = dnxt;
                    dij_prv[xn][yn] = std::make_pair(x, y);
                    pq.emplace(dnxt, xn, yn);
                }
            }
        }
    }
};

using namespace std;
int main()
{
    cin >> H >> W;
    A.resize(H);
    for (int i = 0; i < H; i++) cin >> A[i];

    GridGraph g(H, W);
    g.dijkstra(0, 0);
    cout << g.dij[H - 1][W - 1] << endl;
}
0