結果

問題 No.160 最短経路のうち辞書順最小
ユーザー stack9996stack9996
提出日時 2015-08-25 22:51:34
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,522 bytes
コンパイル時間 1,258 ms
コンパイル使用メモリ 80,240 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-25 18:20:37
合計ジャッジ時間 2,702 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,504 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 7 ms
4,380 KB
testcase_06 AC 10 ms
4,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 3 ms
4,384 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,384 KB
testcase_28 WA -
testcase_29 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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);
	}
	for (int i = 0; i <= n; ++i)std::sort(g[i].begin(), g[i].end());
	dijkstra(s);
	std::vector<int> ans;
	ans.push_back(gg);
	while (cost[gg].second != inf){
		ans.push_back(cost[gg].second);
		gg = cost[gg].second;
	}
	std::reverse(ans.begin(), ans.end());
	std::cout << ans[0];
	for (int i = 1; i < ans.size(); ++i){
		std::cout << " " << ans[i];
	}
	std::cout << std::endl;
	return 0;
}
0