結果

問題 No.1308 ジャンプビーコン
ユーザー りあんりあん
提出日時 2020-12-04 18:59:35
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 292 ms / 4,000 ms
コード長 2,747 bytes
コンパイル時間 8,383 ms
コンパイル使用メモリ 314,028 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-13 09:39:28
合計ジャッジ時間 13,795 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 1 ms
4,368 KB
testcase_04 AC 1 ms
4,368 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 1 ms
4,372 KB
testcase_12 AC 1 ms
4,368 KB
testcase_13 AC 7 ms
4,372 KB
testcase_14 AC 8 ms
4,368 KB
testcase_15 AC 8 ms
4,372 KB
testcase_16 AC 8 ms
4,368 KB
testcase_17 AC 8 ms
4,372 KB
testcase_18 AC 210 ms
4,372 KB
testcase_19 AC 209 ms
4,376 KB
testcase_20 AC 211 ms
4,368 KB
testcase_21 AC 210 ms
4,376 KB
testcase_22 AC 212 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,368 KB
testcase_25 AC 3 ms
4,372 KB
testcase_26 AC 76 ms
4,368 KB
testcase_27 AC 76 ms
4,372 KB
testcase_28 AC 77 ms
4,372 KB
testcase_29 AC 185 ms
4,372 KB
testcase_30 AC 183 ms
4,372 KB
testcase_31 AC 245 ms
4,368 KB
testcase_32 AC 225 ms
4,372 KB
testcase_33 AC 273 ms
4,372 KB
testcase_34 AC 77 ms
4,372 KB
testcase_35 AC 77 ms
4,376 KB
testcase_36 AC 92 ms
4,368 KB
testcase_37 AC 92 ms
4,368 KB
testcase_38 AC 287 ms
4,372 KB
testcase_39 AC 292 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// begin constraints
const int MIN_N = 2;
const int MAX_N = 3000;
const int MIN_Q = 2;
const int MAX_Q = 3000;
const int MIN_C = 1;
const int MAX_C = 1000000000;
const int MIN_L = 1;
const int MAX_L = 1000000000;
// end constraints


class union_find {
    int num;
    vector<int> par, rank;
public:
    union_find(int n) : num(n), par(vector<int>(n)), rank(vector<int>(n)) {
        for (int i = 0; i < n; ++i)
            par[i] = i;
    }
private:
    int find(int x) {
        return par[x] == x ? x : (par[x] = find(par[x]));
    }
public:
    bool unite(int x, int y) {
        x = find(x); y = find(y);
        if (x == y) return false;
        --num;
        if (rank[x] < rank[y]) {
            par[x] = y;
        }
        else {
            par[y] = x;
            if (rank[x] == rank[y]) ++rank[x];
        }
        return true;
    }
};



vector<vector<P>> edge;

long long dfs(int p, int par, int to) {
    if (p == to) return 0;
    for (auto& e : edge[p]) {
        if (e.second == par) continue;
        long long d = dfs(e.second, p, to) + e.first;
        if (d < LM) return d;
    }
    return LM;
}

vector<long long> dp;

long long dfs(int p, int par, long long l, long long ds) {
    dp[p] += min(l, ds);
    for (auto& e : edge[p]) {
        if (e.second == par) continue;
        dp[p] = min(dp[p], dfs(e.second, p, l + e.first, ds));
    }
    return dp[p];
}



int main(int argc, char** argv) {
    registerValidation(argc, argv);
    int n = inf.readInt(MIN_N, MAX_N, "N");
    inf.readSpace();
    int q = inf.readInt(MIN_Q, MAX_Q, "Q");
    inf.readSpace();
    int c = inf.readInt(MIN_C, MAX_C, "C");
    inf.readEoln();

    edge = vector<vector<P>>(n);

    union_find uf(n);
    for (int i = 0; i < n - 1; ++i) {
        int u = inf.readInt(1, n, "U");
        inf.readSpace();
        int v = inf.readInt(1, n, "V");
        inf.readSpace();
        int l = inf.readInt(MIN_L, MAX_L, "L");
        inf.readEoln();
        --u;
        --v;
        ensure(uf.unite(u, v));

        edge[u].emplace_back(l, v);
        edge[v].emplace_back(l, u);
    }
    vector<int> x = inf.readInts(q, 1, n, "X");
    inf.readEoln();
    for (int i = 0; i < q - 1; ++i) {
        ensure(x[i] != x[i + 1]);
    }
    inf.readEof();


    for (int i = 0; i < q; ++i) {
        --x[i];
    }

    dp = vector<long long>(n, LM);
    dp[x[0]] = 0;
    for (int i = 1; i < q; ++i) {
        long long d = dfs(x[i - 1], -1, x[i]);
        dfs(x[i], -1, c, d);
    }
    long long ans = LM;
    for (int i = 0; i < n; ++i) {
        ans = min(ans, dp[i]);
    }
    cout << ans << '\n';

    return 0;
}
0