結果

問題 No.1308 ジャンプビーコン
ユーザー trineutrontrineutron
提出日時 2020-12-05 05:45:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,599 bytes
コンパイル時間 1,970 ms
コンパイル使用メモリ 210,612 KB
実行使用メモリ 144,436 KB
最終ジャッジ日時 2023-10-13 13:45:43
合計ジャッジ時間 27,139 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,832 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 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,352 KB
testcase_09 AC 2 ms
4,352 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 22 ms
8,444 KB
testcase_14 AC 21 ms
8,532 KB
testcase_15 AC 27 ms
8,448 KB
testcase_16 AC 21 ms
8,584 KB
testcase_17 AC 20 ms
8,504 KB
testcase_18 AC 3,158 ms
143,912 KB
testcase_19 AC 3,231 ms
144,060 KB
testcase_20 AC 3,601 ms
144,432 KB
testcase_21 AC 3,431 ms
143,964 KB
testcase_22 AC 3,150 ms
144,044 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 757 ms
143,976 KB
testcase_27 AC 778 ms
144,436 KB
testcase_28 AC 778 ms
143,980 KB
testcase_29 TLE -
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[v]) {
        if (next == par) continue;
        dist[next] = dist[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[u].emplace_back(v, l);
        to[v].emplace_back(u, l);
    }
    vector<int> x(q);
    for (int i = 0; i < q; i++) {
        cin >> x[i];
        x[i]--;
    }
    vector dist(n, vector(n, int64_t()));
    for (int i = 0; i < n; i++) {
        dfs(to, dist[i], i, -1);
    }

    vector dp(q, vector(n, inf));
    dp.at(0).at(x.at(0)) = 0;
    for (int i = 1; i < q; i++) {
        int64_t mincost_prev = *min_element(dp[i - 1].begin(), dp[i - 1].end());
        for (int j = 0; j < n; j++) {
            chmin(dp[i][j], dp[i - 1][j] + dist[x[i - 1]][x[i]]);
            if (dist[x[i - 1]][j] + dist[j][x[i]] != dist[x[i - 1]][x[i]]) continue;
            chmin(dp[i][j], mincost_prev + dist[x[i - 1]][x[i]]);
            for (int k = 0; k < n; k++) {
                if (dist[k][j] + dist[j][x[i]] != dist[k][x[i]]) continue;
                chmin(dp[i][j], dp[i - 1][k] + c + dist[k][x[i]]);
            }
        }
    }
    cout << dp[q - 1][x[q - 1]] << endl;
}
0