結果
問題 | No.1545 [Cherry 2nd Tune N] Anthem |
ユーザー |
|
提出日時 | 2021-06-12 06:01:58 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 150 ms / 3,000 ms |
コード長 | 2,477 bytes |
コンパイル時間 | 2,539 ms |
コンパイル使用メモリ | 216,188 KB |
最終ジャッジ日時 | 2025-01-22 07:45:26 |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 67 |
ソースコード
#include <bits/stdc++.h>using namespace std;#include <math.h>#include <iomanip>#include <cstdint>#include <string>#include <sstream>template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }#define rep(i,n) for (int i = 0; i < (n); ++i)typedef long long ll;using P=pair<ll,ll>;const ll INF=1e18;const int mod=998244353;struct edge{int to;ll w;edge(int to,ll w):to(to),w(w){}};void solve(){int n,s,t,k,m;cin>>n>>s>>t>>k;s--;t--;vector<int>x(n);rep(i,n){cin>>x[i];}cin>>m;vector<vector<edge>>G(n);rep(i,m){int a,b,y;cin>>a>>b>>y;a--;b--;G[a].emplace_back(b,x[b]+y);}vector<vector<ll>>dist(k+1,vector<ll>(n,INF));vector<vector<int>>prev(k+1,vector<int>(n,-1));vector<vector<int>>prec(k+1,vector<int>(n,-1));priority_queue<pair<ll,pair<int,int>>,vector<pair<ll,pair<int,int>>>,greater<pair<ll,pair<int,int>>>>q;q.push(make_pair(x[s],make_pair(0,s)));dist[0][s]=x[s];while(!q.empty()){int c=q.top().second.first;int v=q.top().second.second;ll cost=q.top().first;q.pop();if(cost!=dist[c][v]){continue;}for(auto u:G[v]){int nc=min(k-1,c+1);if(chmin(dist[nc][u.to],cost+u.w)){prev[nc][u.to]=v;prec[nc][u.to]=c;q.push(make_pair(dist[nc][u.to],make_pair(nc,u.to)));}}}if(dist[k-1][t]==INF){cout<<"Impossible"<<endl;return;}cout<<"Possible"<<endl;cout<<dist[k-1][t]<<endl;vector<int>ans;int p=t,pc=k-1;while(pc!=-1){ans.push_back(p);int np=pc;pc=prec[np][p];p=prev[np][p];}reverse(ans.begin(),ans.end());cout<<ans.size()<<endl;rep(i,ans.size()){printf("%d%c",ans[i]+1,(i+1<ans.size()?' ':'\n'));}}int main(){ios_base::sync_with_stdio(false);cin.tie(NULL);solve();return 0;}