結果
| 問題 |
No.1308 ジャンプビーコン
|
| コンテスト | |
| ユーザー |
Kude
|
| 提出日時 | 2020-12-05 08:27:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2,992 ms / 4,000 ms |
| コード長 | 1,955 bytes |
| コンパイル時間 | 2,610 ms |
| コンパイル使用メモリ | 211,264 KB |
| 最終ジャッジ日時 | 2025-01-16 17:27:07 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = (n)-1; i >= 0; --i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
constexpr ll inf = 1e18;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q, c;
cin >> n >> q >> c;
vector<vector<P>> to(n);
rep(i, n - 1) {
int u, v, l;
cin >> u >> v >> l;
--u, --v;
to[u].emplace_back(v, l);
to[v].emplace_back(u, l);
}
VVL dist(n, VL(n, inf));
rep(s, n) {
VI vs{s};
dist[s][s] = 0;
while(!vs.empty()) {
int u = vs.back(); vs.pop_back();
for(auto [v, l]: to[u]) {
if (dist[s][v] != inf) continue;
dist[s][v] = dist[s][u] + l;
vs.push_back(v);
}
}
}
VI x(q);
rep(i, q) cin >> x[i], x[i]--;
VL dp(n, inf);
dp[x[0]] = 0;
rep(i, q - 1) {
VL ndp = dp;
rep(u, n) ndp[u] += dist[x[i]][x[i+1]];
VL d(n, inf);
priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> q;
auto push = [&](int v, ll c) {
if (c < d[v]) {
d[v] = c;
q.emplace(c, v);
}
};
push(x[i], dp[x[i]]);
rep(u, n) push(u, dp[u] + c);
while(!q.empty()) {
auto [cost, u] = q.top(); q.pop();
if (cost != d[u]) continue;
for(auto [v, l]: to[u]) {
push(v, cost + l);
}
}
rep(u, n) chmin(ndp[u], d[u] + dist[u][x[i+1]]);
dp = move(ndp);
}
ll ans = dp[x[q-1]];
cout << ans << endl;
}
Kude