結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー みここみここ
提出日時 2022-12-12 04:13:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,635 bytes
コンパイル時間 1,122 ms
コンパイル使用メモリ 81,464 KB
実行使用メモリ 32,012 KB
最終ジャッジ日時 2024-04-23 22:04:36
合計ジャッジ時間 19,519 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 RE -
testcase_05 AC 160 ms
20,096 KB
testcase_06 AC 25 ms
7,424 KB
testcase_07 AC 36 ms
8,448 KB
testcase_08 RE -
testcase_09 AC 160 ms
18,560 KB
testcase_10 AC 73 ms
11,648 KB
testcase_11 RE -
testcase_12 AC 105 ms
14,464 KB
testcase_13 RE -
testcase_14 AC 92 ms
12,416 KB
testcase_15 RE -
testcase_16 AC 102 ms
13,312 KB
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 47 ms
9,600 KB
testcase_21 AC 102 ms
14,720 KB
testcase_22 AC 97 ms
12,672 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 AC 137 ms
16,256 KB
testcase_35 AC 115 ms
15,408 KB
testcase_36 AC 114 ms
15,264 KB
testcase_37 AC 107 ms
15,232 KB
testcase_38 AC 127 ms
15,744 KB
testcase_39 AC 89 ms
14,208 KB
testcase_40 AC 89 ms
14,336 KB
testcase_41 AC 91 ms
14,464 KB
testcase_42 AC 89 ms
13,952 KB
testcase_43 AC 90 ms
13,824 KB
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <vector>
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];
    }
};

int main()
{
    int n, m;
    cin >> n >> m;
    ll h[100005];
    for (int u = 0; u < n; u++)
    {
        cin >> h[u];
    }
    graph<ll> g(n * 2), r(n * 2);
    for (int u = 0; u < n; u++)
    {
        g.add_directed_edge(u * 2, u * 2 + 1, 0);
        r.add_directed_edge(u * 2 + 1, u * 2, 0);
    }
    for (int i = 0; i < m; i++)
    {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        if (x > y)
        {
            swap(x, y);
        }
        if (h[x] < h[y])
        {
            g.add_directed_edge(x * 2, y * 2 + 1, h[y] - h[x]);
            r.add_directed_edge(y * 2, x * 2 + 1, 0);
        }
        else
        {
            g.add_directed_edge(x * 2 + 1, y * 2, 0);
            r.add_directed_edge(y * 2 + 1, x * 2, h[x] - h[y]);
        }
    }
    ll d0[100005], d1[100005];
    for (int u = 0; u < n * 2; u++)
    {
        d0[u] = d1[u] = -1;
    }
    d0[0] = 0;
    for (int u = 0; u < n * 2; u++)
    {
        if (d0[u] == -1)
        {
            continue;
        }
        for (edge<ll> e : g[u])
        {
            d0[e.to] = max(d0[e.to], d0[u] + e.cost);
        }
    }
    cout << d0[n * 2 - 1] << endl;
    d1[n * 2 - 1] = 0;
    for (int u = n * 2 - 1; u >= 0; u--)
    {
        if (d1[u] == -1)
        {
            continue;
        }
        for (edge<ll> e : r[u])
        {
            d1[e.to] = max(d1[e.to], d1[u] + e.cost);
        }
    }
    cout << d1[0] << endl;
}
0