結果

問題 No.2095 High Rise
ユーザー みここみここ
提出日時 2022-12-12 06:49:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 846 ms / 2,000 ms
コード長 2,605 bytes
コンパイル時間 888 ms
コンパイル使用メモリ 88,200 KB
実行使用メモリ 126,316 KB
最終ジャッジ日時 2024-04-24 00:07:30
合計ジャッジ時間 7,696 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 5 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 88 ms
19,328 KB
testcase_18 AC 55 ms
13,824 KB
testcase_19 AC 14 ms
5,888 KB
testcase_20 AC 95 ms
19,664 KB
testcase_21 AC 188 ms
33,152 KB
testcase_22 AC 811 ms
126,188 KB
testcase_23 AC 846 ms
126,316 KB
testcase_24 AC 811 ms
125,696 KB
testcase_25 AC 808 ms
126,192 KB
testcase_26 AC 809 ms
126,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <queue>
using namespace std;
typedef long long ll;

template <typename T = int>
struct edge
{
    int from, to;
    T cost;
    int id;

    operator int() const
    {
        return to;
    }
};

template <typename T = int>
struct graph
{
    int n, m;
    std::vector<std::vector<edge<T>>> g;

    graph() {}

    graph(int n) : n(n), m(0)
    {
        g.resize(n);
    }

    void add_directed_edge(int from, int to, T cost = 1)
    {
        assert(0 <= from && from < n);
        assert(0 <= to && to < n);
        g[from].push_back((edge<T>){from, to, cost, m++});
    }

    void add_undirected_edge(int from, int to, T cost = 1)
    {
        assert(0 <= from && from < n);
        assert(0 <= to && to < n);
        g[from].push_back((edge<T>){from, to, cost, m});
        g[to].push_back((edge<T>){to, from, cost, m++});
    }

    int size()
    {
        return n;
    }

    int edge_size()
    {
        return m;
    }

    inline const std::vector<edge<T>> &operator[](const int &u) const
    {
        return g[u];
    }

    inline std::vector<edge<T>> &operator[](const int &u)
    {
        return g[u];
    }
};

template <typename T = long long, typename G>
std::vector<T> dijkstra(G &g, int s)
{
    using P = std::pair<T, int>;
    int n = g.size();
    assert(s >= 0 && s < n);
    std::vector<T> d(n, -1);
    std::priority_queue<P, std::vector<P>, std::greater<P>> que;
    d[s] = 0;
    que.push(P(0, s));
    while (que.size())
    {
        auto [dist, u] = que.top();
        que.pop();
        if (d[u] < dist)
        {
            continue;
        }
        for (edge<T> e : g[u])
        {
            int v = e.to;
            if (d[v] == -1 || d[v] > d[u] + e.cost)
            {
                d[v] = d[u] + e.cost;
                que.push(P(d[v], v));
            }
        }
    }
    return d;
}

int a[1005][1005];

int main()
{
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cin >> a[i][j];
        }
    }
    graph<ll> g(n * m + n);
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            g.add_directed_edge(i * m + j, i + n * m, 0);
        }
    }
    for(int i = 0; i < n - 1; i++)
    {
        for(int j = 0; j < m; j++)
        {
            g.add_directed_edge(i + n * m, (i + 1) * m + j, a[i][j] + a[i + 1][j]);
            g.add_directed_edge(i * m + j, (i + 1) * m + j, a[i + 1][j]);
        }
    }
    vector<ll> d = dijkstra(g, n * m);
    cout << d[n * m + n - 1] << endl;
}
0