結果

問題 No.1308 ジャンプビーコン
ユーザー やむなくやむなく
提出日時 2020-12-01 23:05:26
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 403 ms / 4,000 ms
コード長 1,205 bytes
コンパイル時間 2,313 ms
コンパイル使用メモリ 208,068 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-13 09:29:51
合計ジャッジ時間 8,088 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 10 ms
4,352 KB
testcase_14 AC 11 ms
4,348 KB
testcase_15 AC 10 ms
4,352 KB
testcase_16 AC 10 ms
4,348 KB
testcase_17 AC 10 ms
4,348 KB
testcase_18 AC 264 ms
4,352 KB
testcase_19 AC 265 ms
4,352 KB
testcase_20 AC 270 ms
4,348 KB
testcase_21 AC 268 ms
4,348 KB
testcase_22 AC 267 ms
4,352 KB
testcase_23 AC 2 ms
4,352 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 104 ms
4,352 KB
testcase_27 AC 104 ms
4,348 KB
testcase_28 AC 104 ms
4,352 KB
testcase_29 AC 207 ms
4,352 KB
testcase_30 AC 206 ms
4,348 KB
testcase_31 AC 245 ms
4,352 KB
testcase_32 AC 267 ms
4,348 KB
testcase_33 AC 321 ms
4,352 KB
testcase_34 AC 104 ms
4,352 KB
testcase_35 AC 103 ms
4,352 KB
testcase_36 AC 124 ms
4,348 KB
testcase_37 AC 125 ms
4,348 KB
testcase_38 AC 402 ms
4,352 KB
testcase_39 AC 403 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//
// AC solution (writer)
//

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
constexpr ll INF = 1e18;

vector<vector<pair<int, int>>> e;

ll dist(int x, int p, int to) {
    if (x == to) return 0;
    ll ans = INF;
    for (auto &ep : e[x]) {
        if (ep.first == p) continue;
        ans = min(ans, ep.second + dist(ep.first, x, to));
    }
    return ans;
}

vector<ll> dp;

ll dfs(int x, int p, ll d, ll l) {
    dp[x] += min(d, l);
    for (auto &ep : e[x]) {
        if (ep.first == p) continue;
        dp[x] = min(dp[x], dfs(ep.first, x, ep.second + d, l));
    }
    return dp[x];
}

int main() {
    int n, q, c;
    cin >> n >> q >> c;
    e = vector<vector<pair<int, int>>>(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v, l;
        cin >> u >> v >> l;
        u--, v--;
        e[u].emplace_back(v, l);
        e[v].emplace_back(u, l);
    }

    vector<int> x(q);
    for (int i = 0; i < q; i++) {
        cin >> x[i];
        x[i]--;
    }
    dp = vector<ll>(n, INF);
    dp[x[0]] = 0;
    for (int i = 1; i < q; i++) {
        ll l = dist(x[i], x[i], x[i - 1]);
        dfs(x[i], x[i], c, l);
    }
    cout << dp[x[q - 1]] << endl;

    return 0;
}
0