結果

問題 No.1308 ジャンプビーコン
ユーザー りあんりあん
提出日時 2020-12-04 20:44:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 542 ms / 4,000 ms
コード長 2,166 bytes
コンパイル時間 2,413 ms
コンパイル使用メモリ 211,484 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-13 10:27:05
合計ジャッジ時間 10,181 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 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,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 17 ms
4,348 KB
testcase_14 AC 17 ms
4,352 KB
testcase_15 AC 17 ms
4,348 KB
testcase_16 AC 17 ms
4,348 KB
testcase_17 AC 17 ms
4,352 KB
testcase_18 AC 356 ms
4,352 KB
testcase_19 AC 354 ms
4,348 KB
testcase_20 AC 354 ms
4,348 KB
testcase_21 AC 354 ms
4,352 KB
testcase_22 AC 353 ms
4,352 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 2 ms
4,352 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 209 ms
4,352 KB
testcase_27 AC 208 ms
4,348 KB
testcase_28 AC 208 ms
4,352 KB
testcase_29 AC 237 ms
4,348 KB
testcase_30 AC 236 ms
4,352 KB
testcase_31 AC 291 ms
4,348 KB
testcase_32 AC 377 ms
4,352 KB
testcase_33 AC 444 ms
4,348 KB
testcase_34 AC 207 ms
4,352 KB
testcase_35 AC 207 ms
4,348 KB
testcase_36 AC 224 ms
4,352 KB
testcase_37 AC 224 ms
4,348 KB
testcase_38 AC 535 ms
4,356 KB
testcase_39 AC 542 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const long long LM = 1LL << 60;


long long get_dist(int s, int g, const vector<vector<P>>& edge) {
    if (s == g) return 0;
    int n = edge.size();
    vector<long long> dist(n, LM);
    dist[s] = 0;
    queue<int> q;
    q.push(s);
    while (true) {
        int p = q.front();
        if (p == g) return dist[g];
        q.pop();
        for (auto& e : edge[p]) {
            if (dist[e.second] == LM) {
                dist[e.second] = dist[p] + e.first;
                q.push(e.second);
            }
        }
    }
}

void calc(int s, long long l, long long ds, vector<long long>& dp, const vector<vector<P>>& edge) {
    int n = edge.size();
    vector<long long> dist(n, LM);
    vector<bool> vis(n, false);
    stack<int> st;
    st.push(s);
    dist[s] = l;

    while (!st.empty()) {
        int p = st.top();
        if (vis[p]) {
            st.pop();
            for (auto& e : edge[p]) {
                if (dist[p] < dist[e.second]) {
                    dp[p] = min(dp[p], dp[e.second]);
                }
            }
        }
        else {
            vis[p] = true;
            dp[p] += min(dist[p], ds);
            for (auto& e : edge[p]) {
                if (dist[e.second] == LM) {
                    dist[e.second] = dist[p] + e.first;
                    st.push(e.second);
                }
            }
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int n, q, c;
    cin >> n >> q >> c;
    vector<vector<P>> edge(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v, l;
        cin >> u >> v >> l;
        --u;
        --v;
        edge[u].emplace_back(l, v);
        edge[v].emplace_back(l, u);
    }
    vector<long long> dp(n, LM);
    int prev;
    cin >> prev;
    --prev;
    dp[prev] = 0;
    for (int i = 1; i < q; ++i) {
        int x;
        cin >> x;
        --x;
        long long d = get_dist(prev, x, edge);
        calc(x, c, d, dp, edge);
        prev = x;
    }
    long long ans = LM;
    for (int i = 0; i < n; ++i) {
        ans = min(ans, dp[i]);
    }
    cout << ans << '\n';
    return 0;
}
0