結果

問題 No.1308 ジャンプビーコン
ユーザー pes_magicpes_magic
提出日時 2020-12-20 14:06:48
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 599 ms / 4,000 ms
コード長 1,987 bytes
コンパイル時間 1,205 ms
コンパイル使用メモリ 92,332 KB
実行使用メモリ 263,128 KB
最終ジャッジ日時 2023-10-21 10:39:25
合計ジャッジ時間 6,721 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 3 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 9 ms
4,348 KB
testcase_14 AC 8 ms
4,348 KB
testcase_15 AC 9 ms
4,348 KB
testcase_16 AC 9 ms
4,348 KB
testcase_17 AC 9 ms
4,348 KB
testcase_18 AC 156 ms
4,900 KB
testcase_19 AC 152 ms
4,900 KB
testcase_20 AC 154 ms
4,900 KB
testcase_21 AC 154 ms
4,900 KB
testcase_22 AC 154 ms
4,900 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 3 ms
4,348 KB
testcase_25 AC 3 ms
4,348 KB
testcase_26 AC 68 ms
4,348 KB
testcase_27 AC 68 ms
4,348 KB
testcase_28 AC 68 ms
4,348 KB
testcase_29 AC 229 ms
96,712 KB
testcase_30 AC 226 ms
93,876 KB
testcase_31 AC 599 ms
263,128 KB
testcase_32 AC 272 ms
96,800 KB
testcase_33 AC 328 ms
93,436 KB
testcase_34 AC 68 ms
4,348 KB
testcase_35 AC 68 ms
4,348 KB
testcase_36 AC 80 ms
4,432 KB
testcase_37 AC 81 ms
4,376 KB
testcase_38 AC 231 ms
8,084 KB
testcase_39 AC 233 ms
8,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main(){
    int N, Q;
    long long C;
    while(cin >> N >> Q >> C){
        vector<vector<pair<int, long long>>> g(N);
        for(int i=0;i<N-1;i++){
            int u, v, l; cin >> u >> v >> l;
            g[u-1].emplace_back(v-1, l);
            g[v-1].emplace_back(u-1, l);
        }
        vector<int> x(Q);
        for(auto& t : x){
            cin >> t;
            --t;
        }
        long long res = 0;
        vector<int> seq(1, x[0]);
        vector<long long> cost(1, -C);
        for(int i=0;i+1<Q;i++){
            vector<long long> dist(N, -1);
            vector<int> prev(N, -1);
            dist[x[i+1]] = 0;
            queue<int> qu; qu.push(x[i+1]);
            while(!qu.empty()){
                int p = qu.front(); qu.pop();
                for(auto& nd : g[p]){
                    if(dist[nd.first] != -1) continue;
                    dist[nd.first] = dist[p] + nd.second;
                    prev[nd.first] = p;
                    qu.push(nd.first);
                }
            }
            long long whole = dist[x[i]];
            res += whole;
            int cur = x[i];
            while(true){
                int next = prev[cur];
                if(next == -1) break;
                seq.push_back(next);
                cost.push_back(whole-dist[next]-C);
                cur = next;
            }
        }
        vector<vector<int>> posList(N);
        for(int i=0;i<seq.size();i++){
            posList[seq[i]].push_back(i);
        }
        vector<int> next(seq.size(), -1);
        for(auto& v : posList){
            for(int i=0;i+1<v.size();i++) next[v[i]] = v[i+1];
        }
        vector<long long> dp(seq.size(), 0);
        for(int i=0;i+1<seq.size();i++){
            if(next[i] != -1) dp[next[i]] = max(dp[next[i]], dp[i] + cost[next[i]]);
            dp[i+1] = max(dp[i+1], dp[i]);
        }
        cout << res - dp.back() << endl;
    }
}
0