結果

問題 No.1308 ジャンプビーコン
ユーザー KudeKude
提出日時 2020-12-05 08:27:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,871 ms / 4,000 ms
コード長 1,955 bytes
コンパイル時間 4,662 ms
コンパイル使用メモリ 218,504 KB
実行使用メモリ 74,272 KB
最終ジャッジ日時 2023-10-13 16:49:32
合計ジャッジ時間 54,256 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 3 ms
4,352 KB
testcase_10 AC 3 ms
4,352 KB
testcase_11 AC 3 ms
4,352 KB
testcase_12 AC 3 ms
4,348 KB
testcase_13 AC 111 ms
4,348 KB
testcase_14 AC 112 ms
4,352 KB
testcase_15 AC 110 ms
4,352 KB
testcase_16 AC 111 ms
4,348 KB
testcase_17 AC 112 ms
4,348 KB
testcase_18 AC 2,724 ms
73,956 KB
testcase_19 AC 2,728 ms
74,040 KB
testcase_20 AC 2,653 ms
74,048 KB
testcase_21 AC 2,627 ms
74,236 KB
testcase_22 AC 2,637 ms
74,016 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2,861 ms
73,968 KB
testcase_27 AC 2,868 ms
74,016 KB
testcase_28 AC 2,774 ms
74,096 KB
testcase_29 AC 2,126 ms
73,936 KB
testcase_30 AC 2,157 ms
73,988 KB
testcase_31 AC 1,917 ms
74,008 KB
testcase_32 AC 2,014 ms
73,944 KB
testcase_33 AC 2,263 ms
73,956 KB
testcase_34 AC 2,864 ms
74,036 KB
testcase_35 AC 2,871 ms
74,016 KB
testcase_36 AC 2,600 ms
73,968 KB
testcase_37 AC 2,609 ms
74,272 KB
testcase_38 AC 2,762 ms
74,068 KB
testcase_39 AC 2,796 ms
74,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

constexpr ll inf = 1e18;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, q, c;
    cin >> n >> q >> c;
    vector<vector<P>> to(n);
    rep(i, n - 1) {
        int u, v, l;
        cin >> u >> v >> l;
        --u, --v;
        to[u].emplace_back(v, l);
        to[v].emplace_back(u, l);
    }
    VVL dist(n, VL(n, inf));
    rep(s, n) {
        VI vs{s};
        dist[s][s] = 0;
        while(!vs.empty()) {
            int u = vs.back(); vs.pop_back();
            for(auto [v, l]: to[u]) {
                if (dist[s][v] != inf) continue;
                dist[s][v] = dist[s][u] + l;
                vs.push_back(v);
            }
        }
    }
    VI x(q);
    rep(i, q) cin >> x[i], x[i]--;
    VL dp(n, inf);
    dp[x[0]] = 0;
    rep(i, q - 1) {
        VL ndp = dp;
        rep(u, n) ndp[u] += dist[x[i]][x[i+1]];
        VL d(n, inf);
        priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> q;
        auto push = [&](int v, ll c) {
            if (c < d[v]) {
                d[v] = c;
                q.emplace(c, v);
            }
        };
        push(x[i], dp[x[i]]);
        rep(u, n) push(u, dp[u] + c);
        while(!q.empty()) {
            auto [cost, u] = q.top(); q.pop();
            if (cost != d[u]) continue;
            for(auto [v, l]: to[u]) {
                push(v, cost + l);
            }
        }
        rep(u, n) chmin(ndp[u], d[u] + dist[u][x[i+1]]);
        dp = move(ndp);
    }
    ll ans = dp[x[q-1]];
    cout << ans << endl;
}
0