結果

問題 No.1545 [Cherry 2nd Tune N] Anthem
ユーザー kotatsugamekotatsugame
提出日時 2021-06-11 21:50:56
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 290 ms / 3,000 ms
コード長 1,337 bytes
コンパイル時間 1,302 ms
コンパイル使用メモリ 92,408 KB
実行使用メモリ 26,536 KB
最終ジャッジ日時 2024-12-14 23:12:23
合計ジャッジ時間 13,949 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 67
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:9:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    9 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int N,S,T,K,M;
int X[1<<17];
vector<pair<int,int> >G[1<<17];
main()
{
	cin>>N>>S>>T>>K;
	S--,T--;
	K--;
	for(int i=0;i<N;i++)cin>>X[i];
	cin>>M;
	for(int i=0;i<M;i++)
	{
		int a,b,c;cin>>a>>b>>c;
		a--,b--;
		G[a].push_back(make_pair(b,c));
	}
	vector<vector<long> >dp(K+1,vector<long>(N,9e18));
	vector<vector<int> >prc(K+1,vector<int>(N,-1));
	vector<vector<int> >pru(K+1,vector<int>(N,-1));
	dp[0][S]=X[S];
	priority_queue<pair<long,pair<int,int> > >P;
	P.push(make_pair(-X[S],make_pair(0,S)));
	while(!P.empty())
	{
		int c=P.top().second.first;
		int u=P.top().second.second;
		long cost=-P.top().first;
		P.pop();
		if(dp[c][u]<cost)continue;
		for(pair<int,int>e:G[u])
		{
			int v=e.first;
			long nxt=cost+e.second+X[v];
			int nc=c==K?c:c+1;
			if(dp[nc][v]>nxt)
			{
				dp[nc][v]=nxt;
				prc[nc][v]=c;
				pru[nc][v]=u;
				P.push(make_pair(-nxt,make_pair(nc,v)));
			}
		}
	}
	if(dp[K][T]==(long)9e18)
	{
		cout<<"Impossible"<<endl;
		return 0;
	}
	cout<<"Possible"<<endl;
	cout<<dp[K][T]<<endl;
	vector<int>ans;
	while(K!=-1)
	{
		ans.push_back(T);
		int nk=prc[K][T];
		T=pru[K][T];
		K=nk;
	}
	reverse(ans.begin(),ans.end());
	cout<<ans.size()<<endl;
	for(int i=0;i<ans.size();i++)cout<<ans[i]+1<<(i+1==ans.size()?"\n":" ");
}
0