結果

問題 No.1308 ジャンプビーコン
ユーザー ChanyuhChanyuh
提出日時 2021-01-07 12:08:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,108 ms / 4,000 ms
コード長 4,034 bytes
コンパイル時間 1,697 ms
コンパイル使用メモリ 143,688 KB
実行使用メモリ 146,116 KB
最終ジャッジ日時 2024-04-25 15:23:33
合計ジャッジ時間 19,395 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
7,924 KB
testcase_04 AC 3 ms
7,856 KB
testcase_05 AC 3 ms
7,872 KB
testcase_06 AC 3 ms
8,064 KB
testcase_07 AC 3 ms
7,872 KB
testcase_08 AC 5 ms
17,972 KB
testcase_09 AC 5 ms
18,240 KB
testcase_10 AC 6 ms
18,188 KB
testcase_11 AC 5 ms
18,212 KB
testcase_12 AC 5 ms
17,928 KB
testcase_13 AC 29 ms
79,672 KB
testcase_14 AC 29 ms
77,860 KB
testcase_15 AC 27 ms
79,592 KB
testcase_16 AC 26 ms
79,808 KB
testcase_17 AC 26 ms
77,860 KB
testcase_18 AC 867 ms
145,588 KB
testcase_19 AC 790 ms
145,588 KB
testcase_20 AC 815 ms
145,588 KB
testcase_21 AC 824 ms
145,708 KB
testcase_22 AC 817 ms
145,580 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 16 ms
73,620 KB
testcase_25 AC 17 ms
73,572 KB
testcase_26 AC 728 ms
145,616 KB
testcase_27 AC 718 ms
145,396 KB
testcase_28 AC 771 ms
145,492 KB
testcase_29 AC 1,042 ms
146,116 KB
testcase_30 AC 1,058 ms
146,116 KB
testcase_31 AC 863 ms
145,984 KB
testcase_32 AC 753 ms
145,860 KB
testcase_33 AC 1,108 ms
145,988 KB
testcase_34 AC 702 ms
145,496 KB
testcase_35 AC 747 ms
145,488 KB
testcase_36 AC 788 ms
145,776 KB
testcase_37 AC 791 ms
145,648 KB
testcase_38 AC 876 ms
145,664 KB
testcase_39 AC 883 ms
145,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<array>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}

struct LCA{
    int n,root,k=0;
    vector<vector<int>> D,G;
    vector<int> depth;

    void dfs(int s,int par){
        D[s].push_back(par);
        if (par!=-1) depth[s]=depth[par]+1;
        rep(i,G[s].size()){
            int t=G[s][i];
            if (t==par) continue;
            dfs(t,s);
        }
    }

    LCA(int n_,int r_){
        n=n_;root=r_;
        depth.resize(n,0);
        D.resize(n,{});
        G.resize(n,{});
    }
    
    void add_edge(int a,int b){
      G[a].push_back(b);
      G[b].push_back(a);
    }

    void build(){
      dfs(root,-1);
      while(true){
          bool flag=false;
          int m=(1<<k);
          rep(i,n){
              if (D[i][k]==-1) D[i].push_back(-1);
              else{
                  if (D[D[i][k]][k]!=-1) flag=true;
                  D[i].push_back(D[D[i][k]][k]);                }
          }
          if (!flag) break;
          k+=1;
      }
    }

    int lca(int p,int q){
        if (depth[p]<depth[q]) swap(p,q);
        per(i,k+1){
            if (((depth[p]-depth[q]) & (1 << i))!=0) p=D[p][i];
        }
        if (p==q) return p;
        per(i,k+1){
            if (D[p][i]!=D[q][i]) {
                p=D[p][i];
                q=D[q][i];
            }
        }
        return D[p][0];
    } 
};

struct edge{
  int to;ll l;
  edge(){}
  edge(int to,ll l):to(to),l(l){}
};

int n,q;ll c;
vector<edge> G[3010];
ll depth[3010],d[3010][3010];
ll dp[3010][3010];
int x[3010];

void dfs_(int s,int par,ll l){
  if(par!=-1)depth[s]=depth[par]+l;
  for(edge e:G[s]){
    if(e.to==par) continue;
    dfs_(e.to,s,e.l);
  }
}

pair<ll,bool> dfs(int i,int s,int par){
  dp[i+1][s]=dp[i][s]+d[x[i]][x[i+1]];
  bool flag=false;ll z=INF;
  if(s==x[i]) flag=true;
  for(edge e:G[s]){
    if(e.to==par) continue;
    auto pai=dfs(i,e.to,s);
    if(pai.second) flag=true;
    chmin(z,pai.first);
  }
  chmin(z,dp[i][s]+c+d[s][x[i+1]]);
  if(flag){
    chmin(dp[i+1][s],dp[i][x[i]]+d[x[i]][x[i+1]]);
    chmin(dp[i+1][s],z);
  }
  else{
    chmin(dp[i+1][s],z);
  }
  return pair<ll,bool>(z,flag);
}

void solve(){
  cin >> n >> q >> c;
  LCA lca(n,0);
  rep(i,n-1){
    int a,b,l;cin >> a >> b >> l;a--;b--;
    lca.add_edge(a,b);
    G[a].push_back({b,l});
    G[b].push_back({a,l});
  }
  rep(i,q) {
    cin >> x[i];x[i]--;
  }
  dfs_(0,-1,0);
  lca.build();
  rep(i,n){
    rep(j,i){
      d[i][j]=depth[i]+depth[j]-2ll*depth[lca.lca(i,j)];
      d[j][i]=d[i][j];
      //cout << i << " " << j << " " << d[i][j] << endl;
    }
  }
  rep(i,n) dp[0][i]=INF;
  dp[0][x[0]]=0;
  rep(i,q-1){
    dfs(i,x[i+1],-1);
    // rep(j,n){
    //   cout << i+1 << " " << j << " " << dp[i+1][j] << endl;
    // }
  }
  ll ans=INF;
  rep(i,n){
    chmin(ans,dp[q-1][i]);
  }
  cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0