結果

問題 No.160 最短経路のうち辞書順最小
ユーザー jajagacchijajagacchi
提出日時 2017-01-06 15:06:37
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,834 bytes
コンパイル時間 925 ms
コンパイル使用メモリ 86,288 KB
実行使用メモリ 814,460 KB
最終ジャッジ日時 2023-08-22 15:14:24
合計ジャッジ時間 4,603 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
const int INF = 1e9;

struct Edge
{
	int to, cost;
};

class Vertex
{
public:
	int cost;
	int no;
	bool operator>(const Vertex &v) const
	{
		return this->cost > v.cost;
	}
	std::vector<Edge> v_edge;
};

int main()
{
	int N; std::cin >> N;
	int M; std::cin >> M;
	int S; std::cin >> S;
	int G; std::cin >> G;
	
	std::vector<Vertex> v_vertex(N);
	for(int m=0; m<M; m++)
	{
		int a; std::cin >> a;
		int b; std::cin >> b;
		int c; std::cin >> c;
		v_vertex[a].v_edge.push_back(Edge{b,c});
		v_vertex[b].v_edge.push_back(Edge{a,c});
	}
	for(int n=0; n<N; n++)
	{
		v_vertex[n].no = n;
		v_vertex[n].cost = INF;
	}
	v_vertex[S].cost = 0;
	std::priority_queue<Vertex , std::vector<Vertex> , std::greater<Vertex> > q;
	q.push(v_vertex[S]);

	while(!q.empty())
	{
		Vertex v = q.top(); q.pop();
		if(v_vertex[v.no].cost < v.cost) continue;
		for(auto &x : v.v_edge)
		{
			if(v_vertex[x.to].cost > v.cost+x.cost)
			{
				v_vertex[x.to].cost = v.cost+x.cost;
				q.push(v_vertex[x.to]);
			}
		}
	}
	std::cout << v_vertex[G].cost << std::endl;

	std::stack<std::vector<int> > st;
	std::vector<int> tmp; tmp.push_back(G);
	st.push(tmp);
	std::vector<std::vector<int> > ans;
	while(!st.empty())
	{
		std::vector<int> current = st.top(); st.pop();
		if(current[current.size()-1]==S)
		{
			ans.push_back(current);
		}
		
		for(auto &x : v_vertex[current[current.size()-1]].v_edge)
		{
			if(v_vertex[x.to].cost+x.cost==v_vertex[current[current.size()-1]].cost)
			{
				std::vector<int> tmp = current;
				tmp.push_back(x.to);
				st.push(tmp);
			}
		}
	}
	for(auto &x : ans)
	{
		std::reverse(x.begin() , x.end());
	}
	std::vector<int> Ans = *std::min_element(ans.begin() , ans.end());
	for(auto &x : Ans)
	{
		std::cout << x << " ";
	}
	return 0;
}
0