結果

問題 No.160 最短経路のうち辞書順最小
ユーザー stack9996
提出日時 2015-08-25 23:04:57
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 1,299 bytes
コンパイル時間 587 ms
コンパイル使用メモリ 72,712 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-18 14:32:45
合計ジャッジ時間 1,491 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <vector>
#include <functional>
#include <string>
#include <algorithm>

const int max = 205;
const int inf = 10000000;

typedef std::pair<int, int> P;

P cost[max];
std::vector<P> g[max];
int n, s, gg;

void dijkstra(int k){
	std::priority_queue < P, std::vector<P>, std::greater<P> > que;
	for (int i = 0; i < n; ++i)cost[i].first = inf, cost[i].second = inf;
	cost[k].first = 0;
	que.push(P(0, k));
	while (!que.empty()){
		P p = que.top();
		que.pop();
		int v = p.second;
		if (cost[v].first < p.first)continue;
		for (int i = 0; i < g[v].size(); ++i){
			P e = g[v][i];
			if (cost[e.first].first == cost[v].first + e.second){
				cost[e.first].second = std::min(cost[e.first].second, v);
			}
			if (cost[e.first].first > cost[v].first + e.second){
				cost[e.first].first = cost[v].first + e.second;
				cost[e.first].second = v;
				que.push(P(cost[e.first].first, e.first));
			}
		}
	}
}

int main(){
	int m;
	std::cin >> n >> m >> s >> gg;
	for (int i = 0; i < m; ++i){
		int a, b, c;
		std::cin >> a >> b >> c;
		P p(b, c), q(a, c);
		g[a].push_back(p);
		g[b].push_back(q);
	}
	dijkstra(gg);
	std::cout << s;
	while (cost[s].second != inf){
		std::cout << " " << cost[s].second;
		s = cost[s].second;
	}
	std::cout << std::endl;
	return 0;
}
0