結果

問題 No.1308 ジャンプビーコン
ユーザー trineutrontrineutron
提出日時 2020-12-06 01:10:01
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,394 ms / 4,000 ms
コード長 1,674 bytes
コンパイル時間 2,451 ms
コンパイル使用メモリ 208,884 KB
実行使用メモリ 145,260 KB
最終ジャッジ日時 2023-10-14 13:15:23
合計ジャッジ時間 26,330 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 21 ms
8,488 KB
testcase_14 AC 20 ms
8,504 KB
testcase_15 AC 20 ms
8,448 KB
testcase_16 AC 19 ms
8,500 KB
testcase_17 AC 18 ms
8,512 KB
testcase_18 AC 1,186 ms
144,532 KB
testcase_19 AC 1,179 ms
144,676 KB
testcase_20 AC 1,182 ms
144,480 KB
testcase_21 AC 1,142 ms
144,472 KB
testcase_22 AC 1,144 ms
144,448 KB
testcase_23 AC 1 ms
4,356 KB
testcase_24 AC 3 ms
4,352 KB
testcase_25 AC 2 ms
4,356 KB
testcase_26 AC 1,037 ms
144,444 KB
testcase_27 AC 1,034 ms
144,656 KB
testcase_28 AC 997 ms
144,524 KB
testcase_29 AC 1,307 ms
145,044 KB
testcase_30 AC 1,316 ms
145,260 KB
testcase_31 AC 973 ms
145,124 KB
testcase_32 AC 1,238 ms
145,152 KB
testcase_33 AC 1,394 ms
145,072 KB
testcase_34 AC 1,030 ms
144,576 KB
testcase_35 AC 1,022 ms
144,432 KB
testcase_36 AC 1,016 ms
144,608 KB
testcase_37 AC 1,021 ms
144,428 KB
testcase_38 AC 1,166 ms
144,496 KB
testcase_39 AC 1,182 ms
144,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template <typename T>
void chmin(T &a, T b) {
    if (b < a) {
        a = b;
    }
}

using graph = vector<vector<pair<int64_t, int64_t>>>;
using grid = vector<vector<int64_t>>;
using vec = vector<int64_t>;

constexpr int64_t inf = 1e18;
int64_t n, c;

void dfs(const graph &to, vector<int64_t> &dist, int v, int par) {
    for (auto [next, l] : to[v]) {
        if (next == par) continue;
        dist[next] = dist[v] + l;
        dfs(to, dist, next, v);
    }
}

void treedp(const graph &to, const grid &dist, const vec &prev, vec &next,
            vec &tele, int x0, int x1, int v, int par) {
    tele[v] = prev[v] + c + dist[v][x1];
    for (auto [child, _] : to[v]) {
        if (child == par) continue;
        treedp(to, dist, prev, next, tele, x0, x1, child, v);
        chmin(tele[v], tele[child]);
    }
    chmin(next[v], prev[v] + dist[x0][x1]);
    chmin(next[v], prev[x0] + dist[x0][v] + dist[v][x1]);
    chmin(next[v], tele[v]);
}

int main() {
    int64_t q;
    cin >> n >> q >> c;
    graph to(n);
    for (int i = 0; i < n - 1; i++) {
        int64_t u, v, l;
        cin >> u >> v >> l;
        u--; v--;
        to[u].emplace_back(v, l);
        to[v].emplace_back(u, l);
    }
    vector<int> x(q);
    for (int i = 0; i < q; i++) {
        cin >> x[i];
        x[i]--;
    }
    grid dist(n, vec(n));
    for (int i = 0; i < n; i++) {
        dfs(to, dist[i], i, -1);
    }

    grid dp(q, vec(n, inf));
    dp[0][x[0]] = 0;
    vec tele(n);
    for (int i = 1; i < q; i++) {
        treedp(to, dist, dp[i - 1], dp[i], tele, x[i - 1], x[i], x[i], -1);
    }
    cout << dp[q - 1][x[q - 1]] << endl;
}
0