結果

問題 No.160 最短経路のうち辞書順最小
ユーザー やまぞうやまぞう
提出日時 2015-04-09 01:11:51
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 1,408 bytes
コンパイル時間 857 ms
コンパイル使用メモリ 81,556 KB
実行使用メモリ 5,908 KB
最終ジャッジ日時 2023-09-17 18:57:18
合計ジャッジ時間 2,128 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <vector>

int N;
int M;
int S;
int G;
std::map<std::pair<int, int>, int> cost;
const int INF = 2000000;
std::vector<int> dist;
std::vector<bool> used;
std::vector<int> next;

int getCost(int u, int v)
{
	if (cost.find(std::pair<int, int>(u, v)) == cost.end()) {
		return INF;
	}
	return cost[std::pair<int, int>(u, v)];
}

void set_nmsg(int n, int m, int s, int g)
{
	N = n;
	M = m;
	S = s;
	G = g;
}

void set_abc(int a, int b, int c)
{
	cost[std::pair<int, int>(a, b)] = c;
	cost[std::pair<int, int>(b, a)] = c;
}

void resolve()
{
	dist.resize(N);
	used.resize(N);
	next.resize(N);

	std::fill(dist.begin(), dist.end(), INF);
	std::fill(used.begin(), used.end(), false);
	std::fill(next.begin(), next.end(), -1);
	dist[G] = 0;

	for (;;) {
		int v = -1;
		for (int u = 0; u < N; u++) {
			if (!used[u] && (v == -1 || dist[u] < dist[v])) v = u;
		}
		if (v == -1) break;
		used[v] = true;
		for (int u = 0; u < N; u++) {
			int nd = dist[v] + getCost(v, u);
			if (nd < dist[u] || (nd == dist[u] && v < next[u])) {
				next[u] = v;
				dist[u] = nd;
			}
		}
	}

	int p = S;
	while (p != G) {
		std::cout << p << " ";
		p = next[p];
	}
	std::cout << p << std::endl;
}

int main()
{
	std::cin >> N >> M >> S >> G;
	for (int i = 0; i < M; i++) {
		int a, b, c;
		std::cin >> a >> b >> c;
		set_abc(a, b, c);
	}
	resolve();
}
0