結果

問題 No.1308 ジャンプビーコン
ユーザー trineutrontrineutron
提出日時 2020-12-05 02:24:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,721 bytes
コンパイル時間 3,938 ms
コンパイル使用メモリ 210,096 KB
実行使用メモリ 144,828 KB
最終ジャッジ日時 2023-10-13 13:13:01
合計ジャッジ時間 19,461 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 12 ms
8,472 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 737 ms
144,308 KB
testcase_22 AC 721 ms
143,972 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 663 ms
144,040 KB
testcase_27 AC 640 ms
143,968 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 417 ms
144,356 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
権限があれば一括ダウンロードができます

ソースコード

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>>>;

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

int main() {
    constexpr int64_t inf = 1e18;
    int64_t n, q, c;
    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.at(u).emplace_back(v, l);
        to.at(v).emplace_back(u, l);
    }
    vector<int> x(q);
    for (int i = 0; i < q; i++) {
        cin >> x.at(i);
        x.at(i)--;
    }
    vector dist(n, vector(n, int64_t()));
    for (int i = 0; i < n; i++) {
        dfs(to, dist.at(i), i, -1);
    }

    vector dp(q, vector(n, inf));
    dp.at(0).at(x.at(0)) = 0;
    int64_t mincost_prev = 0;
    for (int i = 1; i < q; i++) {
        int64_t mincost = inf;
        for (int j = 0; j < n; j++) {
            chmin(dp.at(i).at(j), dp.at(i - 1).at(j) + dist.at(x.at(i - 1)).at(x.at(i)));
            chmin(dp.at(i).at(j), mincost_prev + dist.at(x.at(i - 1)).at(j) + dist.at(j).at(x.at(i)));
/*            for (int k = 0; k < n; k++) {
                chmin(dp.at(i).at(j), dp.at(i - 1).at(k) + c + dist.at(k).at(j) + dist.at(j).at(x.at(i)));
                }*/
            chmin(dp.at(i).at(j), dp.at(i - 1).at(j) + c + dist.at(j).at(x.at(i)));
            chmin(mincost, dp.at(i).at(j));
        }
        mincost_prev = mincost;
    }
    cout << dp.at(q - 1).at(x.at(q - 1)) << endl;
}
0