結果

問題 No.1308 ジャンプビーコン
ユーザー りあんりあん
提出日時 2020-12-02 18:28:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,060 ms / 4,000 ms
コード長 2,105 bytes
コンパイル時間 2,749 ms
コンパイル使用メモリ 215,920 KB
実行使用メモリ 74,232 KB
最終ジャッジ日時 2023-10-13 09:37:49
合計ジャッジ時間 57,169 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 3 ms
4,368 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,352 KB
testcase_12 AC 3 ms
4,352 KB
testcase_13 AC 121 ms
4,352 KB
testcase_14 AC 122 ms
4,352 KB
testcase_15 AC 120 ms
4,352 KB
testcase_16 AC 121 ms
4,352 KB
testcase_17 AC 122 ms
4,348 KB
testcase_18 AC 2,969 ms
74,044 KB
testcase_19 AC 2,990 ms
73,988 KB
testcase_20 AC 2,903 ms
74,076 KB
testcase_21 AC 2,870 ms
74,072 KB
testcase_22 AC 2,831 ms
74,140 KB
testcase_23 AC 1 ms
4,352 KB
testcase_24 AC 2 ms
4,352 KB
testcase_25 AC 3 ms
4,348 KB
testcase_26 AC 3,033 ms
74,104 KB
testcase_27 AC 3,039 ms
73,988 KB
testcase_28 AC 2,964 ms
73,996 KB
testcase_29 AC 2,242 ms
74,088 KB
testcase_30 AC 2,243 ms
73,980 KB
testcase_31 AC 1,982 ms
73,968 KB
testcase_32 AC 2,163 ms
74,024 KB
testcase_33 AC 2,375 ms
73,992 KB
testcase_34 AC 3,033 ms
74,000 KB
testcase_35 AC 3,031 ms
73,984 KB
testcase_36 AC 2,771 ms
74,232 KB
testcase_37 AC 2,848 ms
74,072 KB
testcase_38 AC 3,054 ms
73,992 KB
testcase_39 AC 3,060 ms
74,076 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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].push_back({l, v});
        edge[v].push_back({l, u});
    }
    vector<int> x(q);
    for (int i = 0; i < q; ++i) {
        cin >> x[i];
        --x[i];
    }
    vector<vector<long long>> dist(n, vector<long long>(n, LM));
    for (int i = 0; i < n; ++i) {
        queue<int> que;
        dist[i][i] = 0;
        que.push(i);
        while (!que.empty()) {
            int p = que.front();
            que.pop();
            for (auto& e : edge[p]) {
                if (dist[i][e.second] > dist[i][p] + e.first) {
                    dist[i][e.second] = dist[i][p] + e.first;
                    que.push(e.second);
                }
            }
        }
    }
    vector<long long> dp(n, LM);
    dp[x[0]] = 0;
    for (int i = 1; i < q; ++i) {
        vector<long long> d(n, LM);
        for (int j = 0; j < n; ++j) {
            d[j] = min(d[j], dp[j] + c);
            d[x[i - 1]] = min(d[x[i - 1]], dp[j]);
        }
        priority_queue<P, vector<P>, greater<P>> que;
        for (int j = 0; j < n; ++j) {
            que.push({d[j], j});
        }
        while (!que.empty()) {
            P p = que.top();
            que.pop();
            if (p.first > d[p.second]) continue;
            for (auto& e : edge[p.second]) {
                if (d[e.second] > p.first + e.first) {
                    d[e.second] = p.first + e.first;
                    que.push({d[e.second], e.second});
                }
            }
        }
        for (int j = 0; j < n; ++j) {
            dp[j] = min(dp[j] + min(dist[x[i - 1]][x[i]], c + dist[j][x[i]]), d[j] + dist[j][x[i]]);
        }
    }
    long long ans = LM;
    for (int i = 0; i < n; ++i) {
        ans = min(ans, dp[i]);
    }
    cout << ans << '\n';

    return 0;
}
0