結果

問題 No.1308 ジャンプビーコン
コンテスト
ユーザー 👑 Nachia
提出日時 2020-12-05 00:40:47
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 370 ms / 4,000 ms
コード長 1,246 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,390 ms
コンパイル使用メモリ 216,220 KB
実行使用メモリ 191,688 KB
最終ジャッジ日時 2026-06-15 16:29:42
合計ジャッジ時間 8,779 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0;i<(n);i++)

const LL INF = 1000000000000000000;

struct Edge{ int to; LL d;};

int N,Q; LL C;
vector<Edge> E[3000];
int X[3000];
LL D[3000][3000];
LL P[3000][3000];
vector<int> order[3000];

int main(){
  scanf("%d%d%lld",&N,&Q,&C);
  rep(i,N-1){
    int u,v; LL d; scanf("%d%d%lld",&u,&v,&d); u--; v--;
    E[u].push_back({v,d});
    E[v].push_back({u,d});
  }
  rep(i,Q){ scanf("%d",&X[i]); X[i]--; }

  rep(i,N) rep(j,N) D[i][j]=P[i][j]=-1;
  rep(s,N){
    auto& Q = order[s];
    D[s][s]=0; Q.push_back(s);
    for(int i=0; i<Q.size(); i++){
      int p=Q[i];
      for(Edge e:E[p]){
        if(D[s][e.to]!=-1) continue;
        D[s][e.to] = D[s][p] + e.d;
        P[s][e.to] = p;
        Q.push_back(e.to);
      }
    }
  }

  LL T[3000]; rep(i,N) T[i]=INF;
  T[X[0]]=0;

  rep(i,Q-1){
    int s=X[i], t=X[i+1];
    LL buf[3000]; rep(i,N) buf[i]=T[i]+C;
    buf[s]=T[s];
    for(int p:order[s]) for(Edge e:E[p]){
      buf[e.to] = min(buf[e.to],buf[p]+e.d);
    }
    rep(i,N) buf[i]+=D[i][t];
    rep(i,N) buf[i]=min(buf[i],T[i]+D[s][t]);
    rep(i,N) T[i]=buf[i];
  }

  printf("%lld\n",T[X[Q-1]]);

  return 0;
}
0