結果

問題 No.1308 ジャンプビーコン
ユーザー carrot46
提出日時 2020-12-05 14:56:51
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 500 ms / 4,000 ms
コード長 2,619 bytes
コンパイル時間 2,113 ms
コンパイル使用メモリ 182,744 KB
実行使用メモリ 74,496 KB
最終ジャッジ日時 2024-09-15 17:42:20
合計ジャッジ時間 9,126 ms
ジャッジサーバーID
(参考情報)
judge4 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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