結果
問題 |
No.1308 ジャンプビーコン
|
ユーザー |
![]() |
提出日時 | 2021-01-07 12:08:35 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,134 ms / 4,000 ms |
コード長 | 4,034 bytes |
コンパイル時間 | 1,760 ms |
コンパイル使用メモリ | 140,060 KB |
最終ジャッジ日時 | 2025-01-17 10:11:43 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#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(); }