結果

問題 No.160 最短経路のうち辞書順最小
ユーザー TwizzTwizz
提出日時 2017-05-25 21:41:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,355 bytes
コンパイル時間 1,342 ms
コンパイル使用メモリ 162,992 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 23:21:26
合計ジャッジ時間 2,403 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include"bits/stdc++.h"

//#include<bits/stdc++.h>
using namespace std;
#define print(x) cout<<x<<endl;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a) for(int i=0;i<a;i++)
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<int, PI> V;
typedef vector<int> VE;
const ll mod = 100000000;

int n, m, s, g;
int a[20000], b[20000], c[20000];
int d[202];
bool used[202];
int cost[202][202];
int pr[202];

void dijkstra(int s) {
	REP(i, n)d[i] = mod;
	REP(i, n)used[i] = 0;
	d[s] = 0;

	while (true) {
		int v = -1;
		REP(u, n) {
			if (!used[u] && (v == -1 || d[u] < d[v]))v = u;
		}
		if (v == -1)break;
		used[v] = true;
		REP(u, n) {
			if (d[u] > d[v] + cost[v][u]) {
				d[u] = d[v] + cost[v][u];
				pr[u] = v;
			}
			else if (d[u] == d[v] + cost[v][u]) {
				pr[u] = min(pr[u], v);
			}
		}
	}
}

VE get_path(int t) {
	VE path;
	for (; t != -1; t = pr[t])path.push_back(t);
	reverse(path.begin(), path.end());
	return path;
}

int main() {
	cin >> n >> m >> s >> g;
	REP(i, 202)REP(j, 202)cost[i][j] = mod;
	REP(i, m) {
		cin >> a[i] >> b[i] >> c[i];
		cost[a[i]][b[i]] = c[i];
		cost[b[i]][a[i]] = c[i];
	}
	dijkstra(g);
	//VE v = get_path(s);
	VE v;
	int now = s;
	int i = 0;
	while (now != g) {
		v.push_back(now);
		now = pr[now];
	}
	v.push_back(g);
	//print(d[g]);
	REP(i,v.size())cout<<v[i] << " ";
	cout << endl;
	return 0;
}
0