結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー 沙耶花
提出日時 2021-06-11 21:37:24
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 162 ms / 3,000 ms
コード長 1,539 bytes
コンパイル時間 4,794 ms
コンパイル使用メモリ 256,236 KB
最終ジャッジ日時 2025-01-22 05:36:30
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 67
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:18:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   18 |         rep(i,N)scanf("%lld",&X[i]);
      |                 ~~~~~^~~~~~~~~~~~~~
main.cpp:29:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   29 |                 scanf("%d %d %lld",&A,&to[i],&cost[i]);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 100000000000000000

int main(){
	
	int N,S,T,K;
	cin>>N>>S>>T>>K;
	
	S--;T--;
	
	vector<long long> X(N);
	rep(i,N)scanf("%lld",&X[i]);
	
	int M;
	cin>>M;
	
	vector<int> to(M);
	vector<long long> cost(M);
	vector<vector<int>> E(N);
	
	rep(i,M){
		int A;
		scanf("%d %d %lld",&A,&to[i],&cost[i]);
		to[i]--;
		A--;
		E[A].push_back(i);
	}
	
	vector<long long> dis(N*(K+1),Inf);
	priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>> Q;
	Q.emplace(X[S],S+N);
	dis[S+N] = X[S];
	
	vector<int> pre(N*(K+1),-1);
	
	while(Q.size()>0){
		long long D = Q.top().first;
		int u = Q.top().second%N;
		int cnt = Q.top().second/N;
		Q.pop();
		
		if(dis[u+cnt*N]!=D)continue;
		
		rep(i,E[u].size()){
			int v = to[E[u][i]];
			long long D2 = D + cost[E[u][i]] + X[v];
			int nxt = min(K,cnt+1)*N + v;
			if(dis[nxt]>D2){
				dis[nxt] = D2;
				Q.emplace(D2,nxt);
				pre[nxt] = cnt*N + u;
			}
		}
	}

	int cur = T+K*N;
	
	if(dis[cur]==Inf){
		cout<<"Impossible"<<endl;
		return 0;
	}
	cout<<"Possible"<<endl;
	cout<<dis[cur]<<endl;
	vector<int> ans(1,cur);
	
	
	while(pre[cur] != -1){
		cur = pre[cur];
		ans.push_back(cur);
	}
	
	reverse(ans.begin(),ans.end());
	cout<<ans.size()<<endl;
	
	rep(i,ans.size()){
		ans[i] = ans[i]%N;
		ans[i]++;
		if(i!=0)cout<<' ';
		cout<<ans[i];
	}
	cout<<endl;
	
	return 0;
}
0