結果

問題 No.1308 ジャンプビーコン
ユーザー kzyKTkzyKT
提出日時 2020-12-05 00:31:43
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,496 bytes
コンパイル時間 1,692 ms
コンパイル使用メモリ 150,176 KB
実行使用メモリ 126,656 KB
最終ジャッジ日時 2023-10-13 12:27:42
合計ジャッジ時間 41,929 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
46,672 KB
testcase_01 AC 12 ms
42,296 KB
testcase_02 AC 12 ms
42,376 KB
testcase_03 AC 13 ms
44,732 KB
testcase_04 AC 13 ms
44,712 KB
testcase_05 AC 12 ms
44,720 KB
testcase_06 AC 13 ms
44,676 KB
testcase_07 AC 13 ms
44,652 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 17 ms
54,924 KB
testcase_12 AC 17 ms
54,920 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 222 ms
120,584 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 3,079 ms
122,396 KB
testcase_22 AC 3,025 ms
122,500 KB
testcase_23 AC 12 ms
42,332 KB
testcase_24 AC 29 ms
120,644 KB
testcase_25 AC 29 ms
120,520 KB
testcase_26 AC 2,240 ms
122,336 KB
testcase_27 AC 2,259 ms
122,340 KB
testcase_28 AC 2,247 ms
122,380 KB
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

ll dep[200010],dep2[200010];
ll par[200010][20];
vector<P> v[200010];
void dfs(ll x,ll y,ll d){
  dep[x]=d;
  for(int i=0; i<v[x].size(); i++){
    ll z=v[x][i].F;if(z==y) continue;
    par[z][0]=x;dfs(z,x,d+1);
  }
}
void dfs2(ll x,ll y,ll d){
  dep2[x]=d;
  for(int i=0; i<v[x].size(); i++){
    ll z=v[x][i].F;if(z==y) continue;
    dfs2(z,x,d+v[x][i].S);
  }
}
int lca(int a,int b){
  if(dep[a]<dep[b]) swap(a,b);
  for(int i=0; i<20; i++){
    if((dep[a]-dep[b])&(1<<i)) a=par[a][i];
  }
  if(a==b) return a;
  for(int i=19;i>=0;i--){
    if(par[a][i]!=par[b][i]) a=par[a][i],b=par[b][i];
  }
  return par[a][0];
}

int n;
void init() {
  memset(par,0,sizeof(par));
  dfs(0,-1,0);
  for(int i=0; i<19; i++)for(int j=0; j<n; j++) par[j][i+1]=par[par[j][i]][i];
  dfs2(0,-1,0);
}
ll D(int x,int y) {
  return dep2[x]+dep2[y]-dep2[lca(x,y)]*2;
}

ll dp[3333][3333];

void Main() {
  ll m,c;
  cin >> n >> m >> c;
  rep(i,n-1) {
    ll x,y,z;
    cin >> x >> y >> z;
    x--,y--;
    v[x].pb(P(y,z));
    v[y].pb(P(x,z));
  }
  ll a[m];
  rep(i,m) {
    cin >> a[i];
    a[i]--;
  }
  init();
  rep(i,m)rep(j,n) dp[i][j]=MAXL;
  dp[0][a[0]]=0;
  REP(i,1,m) {
    ll M=MAXL;
    rep(j,n) {
      dp[i][j]=min(dp[i][j],dp[i-1][j]+D(a[i-1],a[i]));
      dp[i][j]=min(dp[i][j],dp[i-1][j]+(j!=a[i-1]?c:0)+D(j,a[i]));
      dp[i][a[i]]=min(dp[i][a[i]],dp[i-1][j]+(j!=a[i-1]?c:0)+D(j,a[i]));
      M=min(M,dp[i-1][j]);
    }
    rep(j,n) dp[i][j]=min(dp[i][j],M+D(a[i-1],j)+D(j,a[i]));
  }
  ll ans=MAXL;
  rep(i,n) ans=min(ans,dp[m-1][i]);
  pr(ans);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
0