結果
| 問題 |
No.1308 ジャンプビーコン
|
| コンテスト | |
| ユーザー |
りあん
|
| 提出日時 | 2020-12-02 18:28:13 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3,233 ms / 4,000 ms |
| コード長 | 2,105 bytes |
| コンパイル時間 | 2,635 ms |
| コンパイル使用メモリ | 210,008 KB |
| 最終ジャッジ日時 | 2025-01-16 13:24:16 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
ソースコード
#include<bits/stdc++.h>
using namespace std;
using P = pair<long long, int>;
const long long LM = 1LL << 60;
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
int n, q, c;
cin >> n >> q >> c;
vector<vector<P>> edge(n);
for (int i = 0; i < n - 1; ++i) {
int u, v, l;
cin >> u >> v >> l;
--u;
--v;
edge[u].push_back({l, v});
edge[v].push_back({l, u});
}
vector<int> x(q);
for (int i = 0; i < q; ++i) {
cin >> x[i];
--x[i];
}
vector<vector<long long>> dist(n, vector<long long>(n, LM));
for (int i = 0; i < n; ++i) {
queue<int> que;
dist[i][i] = 0;
que.push(i);
while (!que.empty()) {
int p = que.front();
que.pop();
for (auto& e : edge[p]) {
if (dist[i][e.second] > dist[i][p] + e.first) {
dist[i][e.second] = dist[i][p] + e.first;
que.push(e.second);
}
}
}
}
vector<long long> dp(n, LM);
dp[x[0]] = 0;
for (int i = 1; i < q; ++i) {
vector<long long> d(n, LM);
for (int j = 0; j < n; ++j) {
d[j] = min(d[j], dp[j] + c);
d[x[i - 1]] = min(d[x[i - 1]], dp[j]);
}
priority_queue<P, vector<P>, greater<P>> que;
for (int j = 0; j < n; ++j) {
que.push({d[j], j});
}
while (!que.empty()) {
P p = que.top();
que.pop();
if (p.first > d[p.second]) continue;
for (auto& e : edge[p.second]) {
if (d[e.second] > p.first + e.first) {
d[e.second] = p.first + e.first;
que.push({d[e.second], e.second});
}
}
}
for (int j = 0; j < n; ++j) {
dp[j] = min(dp[j] + min(dist[x[i - 1]][x[i]], c + dist[j][x[i]]), d[j] + dist[j][x[i]]);
}
}
long long ans = LM;
for (int i = 0; i < n; ++i) {
ans = min(ans, dp[i]);
}
cout << ans << '\n';
return 0;
}
りあん