結果

問題 No.1308 ジャンプビーコン
ユーザー 👑 hos.lyric
提出日時 2020-12-05 01:32:30
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,864 ms / 4,000 ms
コード長 2,950 bytes
コンパイル時間 1,071 ms
コンパイル使用メモリ 110,480 KB
実行使用メモリ 145,408 KB
最終ジャッジ日時 2024-09-15 09:51:04
合計ジャッジ時間 51,904 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
constexpr Int INF = 1'000'000'000'000'000'000LL;
constexpr int MAX = 3010;
int N, Q;
Int C;
int A[MAX], B[MAX];
Int L[MAX];
int X[MAX];
vector<int> G[MAX];
void dfs(Int *dist, int u, int p, Int d) {
dist[u] = d;
for (const int i : G[u]) {
const int v = A[i] ^ B[i] ^ u;
if (v != p) {
dfs(dist, v, u, d + L[i]);
}
}
}
void shortest(Int *ds) {
using Node = pair<Int, int>;
priority_queue<Node, vector<Node>, greater<Node>> que;
for (int u = 0; u < N; ++u) {
que.emplace(ds[u], u);
}
for (; !que.empty(); ) {
const Int c = que.top().first;
const int u = que.top().second;
que.pop();
if (ds[u] == c) {
for (const int i : G[u]) {
const int v = A[i] ^ B[i] ^ u;
const Int cc = c + L[i];
if (chmin(ds[v], cc)) {
que.emplace(cc, v);
}
}
}
}
}
Int dist[MAX][MAX];
Int dp[MAX][MAX];
Int sub[MAX];
int main() {
for (; ~scanf("%d%d%lld", &N, &Q, &C); ) {
for (int i = 0; i < N - 1; ++i) {
scanf("%d%d%lld", &A[i], &B[i], &L[i]);
--A[i];
--B[i];
}
for (int q = 0; q < Q; ++q) {
scanf("%d", &X[q]);
--X[q];
}
fill(G, G + N, vector<int>{});
for (int i = 0; i < N - 1; ++i) {
G[A[i]].push_back(i);
G[B[i]].push_back(i);
}
for (int u = 0; u < N; ++u) {
dfs(dist[u], u, -1, 0);
}
for (int q = 0; q < Q; ++q) {
fill(dp[q], dp[q] + N, INF);
}
dp[0][X[0]] = 0;
for (int q = 0; q < Q - 1; ++q) {
for (int u = 0; u < N; ++u) {
chmin(dp[q + 1][u], dp[q][u] + dist[X[q]][X[q + 1]]);
}
Int mn = INF;
for (int u = 0; u < N; ++u) {
sub[u] = dp[q][u] + C;
chmin(mn, dp[q][u]);
}
chmin(sub[X[q]], mn);
shortest(sub);
for (int u = 0; u < N; ++u) {
chmin(dp[q + 1][u], sub[u] + dist[u][X[q + 1]]);
}
// pv(dp[q+1],dp[q+1]+N);
}
const Int ans = *min_element(dp[Q - 1], dp[Q - 1] + N);
printf("%lld\n", ans);
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0