結果
問題 | No.1545 [Cherry 2nd Tune N] Anthem |
ユーザー |
|
提出日時 | 2021-06-14 22:32:11 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 145 ms / 3,000 ms |
コード長 | 2,950 bytes |
コンパイル時間 | 2,401 ms |
コンパイル使用メモリ | 213,552 KB |
最終ジャッジ日時 | 2025-01-22 08:16:01 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 67 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long int;using iPair = pair<int,int>;using lPair = pair<ll, ll>;using ivector = vector<int>;using lvector = vector<ll>;using istack = stack<int>;using iqueue = queue<int>;using ivv = vector<vector<int>>;using lvv = vector<vector<ll>>;const int INF = 0x3f3f3f3f;const ll LINF = 0x3f3f3f3f3f3f3f3f;vector<iPair> dir = {{1,0}, {-1,0}, {0,1}, {0,-1}};#define dump(x) cout << #x << " = " << (x) << endl#define ALL(x) begin(x),end(x)#define rep(i,s,e) for(ll i=(s), i_stop=(e); i<i_stop; ++i)#define repRev(i,s,e) for(ll i=(e)-1, i_stop=(s); i>=i_stop; --i)#define range(i,s,n) for(ll i=(s), i_stop=(s)+(n); i<i_stop; ++i)#define rangeRev(i,s,n) for(ll i=(s), i_stop=(s)-(n); i>i_stop; --i)#define foreach(x,container) for(auto &&x:container)template<typename T> bool chmax(T& a, const T b) {if(a<b) {a=b;return true;} return false;}template<typename T> bool chmin(T& a, const T b) {if(a>b) {a=b;return true;} return false;}template<typename T> void printArr(vector<T> &arr){for(auto &x:arr) {cout << x << " ";} cout << endl;}// =====================================================>/**/ll n,m,s,t,k;lvector x;vector<vector<lPair>> g;void solve() {// 爲了方便計算,頂點從0開始// 輸出的時候再加1cin>>n>>s>>t>>k; --s; --t;x = lvector(n+1);range(i, 0, n) cin>>x[i];cin>>m;g = vector<vector<lPair>>(n+1);while(m--) {ll a,b,y; cin>>a>>b>>y; --a; --b;g[a].emplace_back(b, y);}// 包含端點要有 K 個節點// 0, 1, 2, ..., K-1 層// 除開 K-1 層外,其他層都向上建邊// 頂點編號 [0, n), [n, 2n), ..., [(k-1)n, kn)lvector dist(n*k, LINF);lvector prev(n*k, -1);using T = tuple<ll, ll, ll>;priority_queue<T, vector<T>, greater<T>> pq;dist[s] = x[s];pq.emplace(dist[s], s, -1);while(pq.size()) {auto [d, node, p] = pq.top(); pq.pop();if(d > dist[node]) continue;prev[node] = p;if(node == (k-1)*n+t) break;// 鬆弛鄰點ll layer = node / n;ll u = node % n;for(auto [v,w]: g[u]) {ll next_node = v + (layer + (layer!=k-1)) * n;if(chmin(dist[next_node], d+w+x[v])) {pq.emplace(d+w+x[v], next_node, node);}}}if(dist[(k-1)*n+t] == LINF) cout << "Impossible" << endl;else {cout << "Possible" << endl;cout << dist[(k-1)*n+t] << endl;lvector path; ll node = (k-1)*n+t;while(node != -1) {path.push_back(node);node = prev[node];}cout << path.size() << endl;for(auto it=path.rbegin(); it!=path.rend(); ++it) {cout << (*it)%n+1 << " ";}cout << endl;}}int main(){ios::sync_with_stdio(false);cin.tie(0);solve();return 0;}