結果

問題 No.160 最短経路のうち辞書順最小
ユーザー pekempeypekempey
提出日時 2015-08-18 18:54:26
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,429 bytes
コンパイル時間 1,463 ms
コンパイル使用メモリ 152,628 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-25 12:53:50
合計ジャッジ時間 2,950 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 6 ms
4,376 KB
testcase_06 AC 10 ms
4,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 2 ms
4,380 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 2 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,376 KB
testcase_28 WA -
testcase_29 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
using namespace std;
typedef long long ll;
const ll inf = 1e9;
const ll mod = 1e9 + 7;

typedef pair<int, int> P; // to, length
int N, M, S, T;
vector<P> G[200];
int dp[200], pdp[200];

ostream &operator <<(ostream &os, const vector<int> &v) {
	rep (i, v.size()) {
		if (i) cout << " ";
		cout << v[i];
	}
	return os;
}

int main() {
	cin >> N >> M >> S >> T;
	rep (i, M) {
		int a, b, c;
		cin >> a >> b >> c;
		G[a].emplace_back(b, c);
		G[b].emplace_back(a, c);
	}

	rep (i, N) dp[i] = inf, pdp[i] = inf;

	priority_queue<P, vector<P>, greater<P>> q;
	q.emplace(0, S);
	dp[S] = 0;
	pdp[S] = -1;

	while (!q.empty()) {
		P p = q.top(); q.pop();
		int curr = p.second;
		for (auto e : G[curr]) {
			if (dp[e.first] > dp[curr] + e.second) {
				dp[e.first] = dp[curr] + e.second;
				pdp[e.first] = curr;
				q.emplace(dp[e.first], e.first);
			} else if (dp[e.first] == dp[curr] + e.second) {
				if (pdp[e.first] > curr) {
					pdp[e.first] = curr;
					q.emplace(dp[e.first], e.first);		
				}	
			}
		}
	}

	vector<int> path;
	int curr = T;
	while (curr != -1) {
		path.push_back(curr);
		curr = pdp[curr];	
	}
	reverse(path.begin(), path.end());

	cout << path << endl;

	return 0;
}
0