結果
問題 | No.1545 [Cherry 2nd Tune N] Anthem |
ユーザー | SSRS |
提出日時 | 2021-06-11 21:36:51 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 336 ms / 3,000 ms |
コード長 | 2,215 bytes |
コンパイル時間 | 2,359 ms |
コンパイル使用メモリ | 191,736 KB |
実行使用メモリ | 28,328 KB |
最終ジャッジ日時 | 2024-12-14 22:43:27 |
合計ジャッジ時間 | 15,393 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 67 |
ソースコード
#include <bits/stdc++.h> using namespace std; const long long INF = 1000000000000000; int main(){ int N, S, T, K; cin >> N >> S >> T >> K; S--; T--; vector<int> X(N); for (int i = 0; i < N; i++){ cin >> X[i]; } int M; cin >> M; vector<vector<pair<int, int>>> E(N), E2(N); for (int i = 0; i < M; i++){ int A, B, Y; cin >> A >> B >> Y; A--; B--; E[A].push_back(make_pair(Y, B)); E2[B].push_back(make_pair(Y, A)); } vector<vector<long long>> dp(K, vector<long long>(N, INF)); vector<vector<int>> pr1(K, vector<int>(N, -1)); dp[0][S] = X[S]; for (int i = 0; i < K - 1; i++){ for (int j = 0; j < N; j++){ for (auto P : E[j]){ int w = P.second; long long sum = dp[i][j] + P.first + X[w]; if (dp[i + 1][w] > sum){ pr1[i + 1][w] = j; dp[i + 1][w] = sum; } } } } vector<long long> d(N, INF); vector<int> pr2(N); priority_queue<tuple<long long, int, int>, vector<tuple<long long, int, int>>, greater<tuple<long long, int, int>>> pq; pq.push(make_tuple(0, T, -1)); while (!pq.empty()){ long long dd = get<0>(pq.top()); int v = get<1>(pq.top()); int p = get<2>(pq.top()); pq.pop(); if (d[v] == INF){ d[v] = dd; pr2[v] = p; for (auto P : E2[v]){ int w = P.second; if (d[w] == INF){ pq.push(make_tuple(dd + X[v] + P.first, w, v)); } } } } long long mn = INF, t = -1; for (int i = 0; i < N; i++){ long long sum = dp[K - 1][i] + d[i]; if (sum < mn){ mn = sum; t = i; } } if (t == -1){ cout << "Impossible" << endl; } else { vector<int> ans; int t1 = t; ans.push_back(t1); for (int i = K - 1; i >= 1; i--){ int tmp = pr1[i][t1]; ans.push_back(tmp); t1 = tmp; } reverse(ans.begin(), ans.end()); int t2 = t; while (t2 != T){ t2 = pr2[t2]; ans.push_back(t2); } cout << "Possible" << endl; cout << mn << endl; int R = ans.size(); cout << R << endl; for (int i = 0; i < R; i++){ cout << ans[i] + 1; if (i < R - 1){ cout << ' '; } } cout << endl; } }