結果
| 問題 |
No.1308 ジャンプビーコン
|
| コンテスト | |
| ユーザー |
chocorusk
|
| 提出日時 | 2020-12-06 00:13:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 1,717 ms / 4,000 ms |
| コード長 | 2,472 bytes |
| コンパイル時間 | 1,098 ms |
| コンパイル使用メモリ | 131,572 KB |
| 最終ジャッジ日時 | 2025-01-16 18:10:40 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long ll;
typedef pair<int, ll> P;
vector<P> g[3030];
ll d[3030][3030];
void dfs(int r, int x, int p){
for(auto q:g[x]){
int y=q.first;
if(y!=p){
d[r][y]=d[r][x]+q.second;
dfs(r, y, x);
}
}
}
int main()
{
int n, q;
ll c; cin>>n>>q>>c;
for(int i=0; i<n-1; i++){
int u, v; ll l;
cin>>u>>v>>l;
u--; v--;
g[u].push_back({v, l});
g[v].push_back({u, l});
}
for(int i=0; i<n; i++) dfs(i, i, -1);
int x[3030];
for(int i=0; i<q; i++){
cin>>x[i]; x[i]--;
}
vector<int> v;
for(int i=0; i<q-1; i++){
int x0=x[i];
while(x0!=x[i+1]){
v.push_back(x0);
for(auto q:g[x0]){
int y=q.first;
if(d[x0][y]+d[y][x[i+1]]==d[x0][x[i+1]]){
x0=y;
break;
}
}
}
}
v.push_back(x[q-1]);
vector<int> w[3030];
for(int i=0; i<v.size(); i++) w[v[i]].push_back(i);
int ind[3030];
ind[0]=0;
int k=1;
for(int i=0; i<v.size(); i++){
if(v[i]==x[k]){
ind[k++]=i;
}
}
ll d1[9100000];
for(int i=0; i<v.size()-1; i++){
d1[i]=d[v[i]][v[i+1]];
}
ll s[9100000];
s[0]=0;
for(int i=0; i<v.size()-1; i++){
s[i+1]=s[i]+d1[i];
}
int prev[9100000];
fill(prev, prev+(int)v.size(), -1);
ll d2[9100000];
for(int i=0; i<n; i++){
for(int j=1; j<w[i].size(); j++){
int t=lower_bound(ind, ind+q, w[i][j])-ind;
if(t==0 || w[i][j-1]>ind[t-1]) continue;
d2[w[i][j]]=s[ind[t-1]]-s[w[i][j-1]]+c;
prev[w[i][j]]=w[i][j-1];
}
}
ll dp[9100000];
dp[0]=0;
for(int i=1; i<v.size(); i++){
dp[i]=dp[i-1]+d1[i-1];
if(prev[i]!=-1) dp[i]=min(dp[i], dp[prev[i]]+d2[i]);
}
cout<<dp[v.size()-1]<<endl;
return 0;
}
chocorusk