結果

問題 No.1308 ジャンプビーコン
ユーザー trineutrontrineutron
提出日時 2020-12-05 13:26:32
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,780 bytes
コンパイル時間 2,380 ms
コンパイル使用メモリ 211,196 KB
実行使用メモリ 144,540 KB
最終ジャッジ日時 2023-10-13 21:27:45
合計ジャッジ時間 15,425 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,368 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 11 ms
8,520 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 607 ms
144,044 KB
testcase_22 AC 586 ms
144,340 KB
testcase_23 AC 2 ms
4,372 KB
testcase_24 AC 2 ms
4,368 KB
testcase_25 AC 2 ms
4,368 KB
testcase_26 WA -
testcase_27 AC 518 ms
144,008 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 495 ms
144,332 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[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());
        vector<int64_t> with_teleport(n, inf);
        for (int j = 0; j < n; j++) {
            chmin(with_teleport[j], dp[i - 1][j] + c + dist[j][x[i]]);
        }
        for (int j = 0; j < n; j++) {
            chmin(dp[i][j], dp[i - 1][j] + dist[x[i - 1]][x[i]]);
            int64_t diff = dist[x[i - 1]][j] + dist[j][x[i]] - dist[x[i - 1]][x[i]];
            assert(diff % 2 == 0);
            chmin(dp[i][j], mincost_prev + dist[x[i - 1]][x[i]] + diff / 2);
            if (diff) continue;
            if (dist[x[i - 1]][x[i]] + dist[x[i]][j] != dist[x[i - 1]][j]) continue;
            chmin(dp[i][j], with_teleport[x[i]]);
        }
    }
    cout << dp[q - 1][x[q - 1]] << endl;
}
0