結果

問題 No.160 最短経路のうち辞書順最小
ユーザー stack9996stack9996
提出日時 2015-08-25 23:04:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 1,299 bytes
コンパイル時間 1,215 ms
コンパイル使用メモリ 73,100 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-25 18:20:51
合計ジャッジ時間 1,856 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 7 ms
4,380 KB
testcase_06 AC 9 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 14 ms
4,380 KB
testcase_29 AC 2 ms
4,376 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);
	}
	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