結果

問題 No.160 最短経路のうち辞書順最小
ユーザー 古寺いろは古寺いろは
提出日時 2015-03-25 19:41:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 982 bytes
コンパイル時間 1,421 ms
コンパイル使用メモリ 161,468 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-11 10:15:07
合計ジャッジ時間 3,042 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,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 7 ms
4,380 KB
testcase_05 AC 9 ms
4,376 KB
testcase_06 AC 13 ms
4,376 KB
testcase_07 AC 15 ms
4,376 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 9 ms
4,376 KB
testcase_10 AC 5 ms
4,376 KB
testcase_11 AC 5 ms
4,376 KB
testcase_12 AC 7 ms
4,384 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 4 ms
4,380 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 10 ms
4,380 KB
testcase_19 AC 11 ms
4,380 KB
testcase_20 AC 6 ms
4,384 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 4 ms
4,380 KB
testcase_23 AC 8 ms
4,380 KB
testcase_24 AC 8 ms
4,380 KB
testcase_25 AC 6 ms
4,376 KB
testcase_26 AC 7 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 14 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

int main() {
	int N, M, S, G;
	cin >> N >> M >> S >> G;
	vector<vector<pair<int, int>>> es(N);
	for (int i = 0; i < M; i++)
	{
		int a, b, c;
		cin >> a >> b >> c;
		es[a].push_back(make_pair(b, c));
		es[b].push_back(make_pair(a, c));
	}

	int MAX = 99999999;

	vector<pair<int, int>> dp(N);

	for (int i = 0; i < N; i++)
	{
		dp[i] = make_pair(MAX, -1);
		sort(es[i].begin(), es[i].end());
	}
	dp[G] = make_pair(0, -1);

	priority_queue<pair<pair<int, int>,int>> pq;
	pq.push(make_pair(make_pair(0, -1), G));
	while (!pq.empty()){
		auto now = pq.top(); pq.pop();
		int p = now.second;
		if (dp[p] < now.first) continue;
		for (auto to : es[p]){
			int p2 = to.first;
			auto next = make_pair(to.second + dp[p].first, p);
			if (dp[p2] > next){
				dp[p2] = next;
				pq.push(make_pair(next, p2));
			}
		}
	}

	string ans = to_string(S);
	while (S != G){
		S = dp[S].second;
		ans += " " + to_string(S);
	}
	cout << ans << endl;
}

0