結果

問題 No.160 最短経路のうち辞書順最小
ユーザー TAISA_TAISA_
提出日時 2018-06-22 00:24:52
言語 C++11
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,136 bytes
コンパイル時間 1,460 ms
コンパイル使用メモリ 170,276 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-30 17:48:19
合計ジャッジ時間 2,709 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 8 WA * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long int ll;
typedef pair<int,int> P;
const ll MOD=1000000007;
const ll INF=1000000010;
const ll LINF=4000000000000000010LL;
const int MAX=310;
const double EPS=1e-3;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int d[210];
struct edge{int to,cost;};
vector<edge> G[210];
int p[210];
int main(){
	int n,m,s,g;cin>>n>>m>>s>>g;
	for(int i=0;i<m;i++){
		int a,b,c;cin>>a>>b>>c;
		G[a].push_back({b,c});
		G[b].push_back({a,c});
	}
	priority_queue<P,vector<P>,greater<P>> q;
	q.push(P(0,s));
	fill(d,d+n,INF);
	fill(p,p+n,-1);
	d[s]=0;
	while(!q.empty()){
		P pp=q.top();q.pop();
		int v=pp.second;
		for(int i=0;i<G[v].size();i++){
			edge e=G[v][i];
			if(d[e.to]==d[v]+e.cost){
				p[e.to]=min(v,p[e.to]);
			}
			if(d[e.to]>d[v]+e.cost){
				d[e.to]=d[v]+e.cost;
				p[e.to]=v;
				q.push(P(d[e.to],e.to));
			}
		}
	}
	vector<int> ans;
	for(int i=g;i!=s;i=p[i])ans.push_back(i);
	ans.push_back(s);
	reverse(all(ans));
	for(int i=0;i<ans.size();i++){
		cout<<ans[i];
		if(i!=ans.size()-1){
			cout<<" ";
		}
	}
	cout<<endl;
    return 0;
}
0