結果

問題 No.1308 ジャンプビーコン
ユーザー tkmst201
提出日時 2021-01-05 18:21:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 900 ms / 4,000 ms
コード長 1,928 bytes
コンパイル時間 3,267 ms
コンパイル使用メモリ 211,484 KB
最終ジャッジ日時 2025-01-17 09:48:50
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:22:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   22 |                 scanf("%d %d %d", &u, &v, &l);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:28:24: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   28 |         REP(i, Q) scanf("%d", &X[i]), --X[i];
      |                   ~~~~~^~~~~~~~~~~~~

ソースコード

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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//
int main() {
int N, Q, C;
cin >> N >> Q >> C;
vector<vector<pii>> g(N);
REP(i, N - 1) {
int u, v, l;
scanf("%d %d %d", &u, &v, &l);
--u; --v;
g[u].emplace_back(v, l);
g[v].emplace_back(u, l);
}
vector<int> X(Q);
REP(i, Q) scanf("%d", &X[i]), --X[i];
vector dist(N, vector<ll>(N));
REP(i, N) {
auto dfs = [&](auto self, int u, ll d, int par) -> void {
dist[i][u] = d;
for (auto [v, l] : g[u]) {
if (v == par) continue;
self(self, v, d + l, u);
}
};
dfs(dfs, i, 0, -1);
}
vector<ll> dp(N), nextdp; // [i] :=
REP(i, N) dp[i] = dist[X[0]][i];
REP(i, Q - 1) {
// X[i] -> X[i + 1]
nextdp.assign(N, longINF);
REP(j, N) nextdp[j] = dp[j] + dist[X[i]][X[i + 1]]; //
queue<pii> que;
que.emplace(X[i], -1);
vector<int> vtx;
while (!que.empty()) {
auto [u, p] = que.front(); que.pop();
vtx.emplace_back(u);
for (auto [v, _] : g[u]) {
if (v == p) continue;
if (dist[X[i]][v] + dist[v][X[i + 1]] == dist[X[i]][X[i + 1]]) que.emplace(v, u);
}
}
ll mn = *min_element(ALL(dp));
REP(j, vtx.size()) {
if (j > 0) mn += dist[vtx[j - 1]][vtx[j]];
chmin(mn, dp[vtx[j]] + C);
chmin(nextdp[vtx[j]], mn + dist[vtx[j]][X[i + 1]]);
}
swap(dp, nextdp);
}
cout << *min_element(ALL(dp)) << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0