結果

問題 No.1308 ジャンプビーコン
ユーザー trineutrontrineutron
提出日時 2020-12-05 02:26:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,715 bytes
コンパイル時間 2,447 ms
コンパイル使用メモリ 211,028 KB
実行使用メモリ 8,740 KB
最終ジャッジ日時 2023-10-13 13:13:11
合計ジャッジ時間 9,684 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,740 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 1 ms
4,368 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 2 ms
4,368 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 2 ms
4,368 KB
testcase_12 AC 3 ms
4,368 KB
testcase_13 AC 273 ms
8,476 KB
testcase_14 AC 262 ms
8,548 KB
testcase_15 AC 262 ms
8,576 KB
testcase_16 AC 258 ms
8,524 KB
testcase_17 AC 254 ms
8,516 KB
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

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