結果

問題 No.1308 ジャンプビーコン
ユーザー carrot46carrot46
提出日時 2020-12-05 14:56:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 500 ms / 4,000 ms
コード長 2,619 bytes
コンパイル時間 1,784 ms
コンパイル使用メモリ 182,132 KB
実行使用メモリ 74,592 KB
最終ジャッジ日時 2023-10-13 22:01:13
合計ジャッジ時間 9,136 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 6 ms
4,352 KB
testcase_14 AC 6 ms
4,352 KB
testcase_15 AC 7 ms
4,352 KB
testcase_16 AC 7 ms
4,352 KB
testcase_17 AC 7 ms
4,348 KB
testcase_18 AC 318 ms
74,084 KB
testcase_19 AC 317 ms
74,148 KB
testcase_20 AC 319 ms
74,092 KB
testcase_21 AC 320 ms
74,276 KB
testcase_22 AC 319 ms
74,360 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 185 ms
74,116 KB
testcase_27 AC 185 ms
74,164 KB
testcase_28 AC 185 ms
74,104 KB
testcase_29 AC 324 ms
74,592 KB
testcase_30 AC 323 ms
74,348 KB
testcase_31 AC 500 ms
74,456 KB
testcase_32 AC 368 ms
74,340 KB
testcase_33 AC 440 ms
74,340 KB
testcase_34 AC 189 ms
74,164 KB
testcase_35 AC 187 ms
74,120 KB
testcase_36 AC 202 ms
74,120 KB
testcase_37 AC 201 ms
74,340 KB
testcase_38 AC 396 ms
74,128 KB
testcase_39 AC 407 ms
74,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("O2")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T &b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T &b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void print_vector(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}

struct edge{
        int to, cost;
        edge(int a, int b) : to(a), cost(b){}
    };
using ve = vector<edge>;

void bfs(int s, vec &dist, vector<ve> &G){
    dist[s] = 0;
    queue<P> que;
    que.emplace(s, 0);
    while(!que.empty()){
        P p = que.front(); que.pop();
        int v = p.first;
        ll d = p.second;
        for(const edge &e : G[v]){
            if(dist[e.to] == INF){
                dist[e.to] = d + e.cost;
                que.emplace(e.to, dist[e.to]);
            }
        }
    }
}

bool dfs(int p, int v, int t, ve &res, vector<ve> &G){
    if(v == t) return true;
    for(const edge &e : G[v]){
        if(e.to != p){
            res.push_back(e);
            if(dfs(v, e.to, t, res, G)) return true;
            else res.pop_back();
        }
    }
    return false;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>N>>Q>>K;
    mat dist(N, vec(N, INF));
    vector<ve> G(N);
    vec query(Q);
    rep(i, N - 1){
        ll u, v, l; cin>>u>>v>>l; --u; --v;
        G[u].emplace_back(v, l);
        G[v].emplace_back(u, l);
    }
    rep(i, Q) {cin>>query[i]; --query[i];}
    rep(i, N) bfs(i, dist[i], G);
    mat dp(2, vec(N, INF));
    dp[0][query[0]] = 0;
    reps(i, 1, Q){
        int s = query[i-1], t = query[i];
        rep(v, N) dp[1][v] = min(INF, dp[0][v] + dist[s][t]);
        ve course;
        dfs(-1, s, t, course, G);
        ll temp_cost = *min_element(ALL(dp[0]));
        for(const edge &e : course){
            temp_cost += e.cost;
            chmin(temp_cost, dp[0][e.to] + K);
            chmin(dp[1][e.to], temp_cost + dist[e.to][t]);
        }
        swap(dp[0], dp[1]);
    }
    cout<<*min_element(ALL(dp[0]))<<endl;
}
0