結果

問題 No.160 最短経路のうち辞書順最小
ユーザー jajagacchijajagacchi
提出日時 2017-01-07 07:04:56
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,351 bytes
コンパイル時間 949 ms
コンパイル使用メモリ 90,120 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-22 15:40:32
合計ジャッジ時間 2,236 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

struct Edge
{
	int to, cost;
	bool operator>(const Edge &e) const
	{
		return this->to > e.to;
	}
	bool operator<(const Edge &e) const
	{
		return this->to < e.to;
	}
};

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(auto &x : v_vertex)
		{
		//	std::sort(x.v_edge.begin() , x.v_edge.end() , std::greater<Edge>());
			std::sort(x.v_edge.begin() , x.v_edge.end());
		}
	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::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 : current)  */
			/* {                       */
			/* 	std::cout << x << " "; */
			/* }                       */
			/* std::cout << std::endl; */
			break;
		}
		else
		{
			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);
					break;
				}
			}
		}
	}
	
	std::vector<int> Ans = *std::min_element(ans.begin() , ans.end());
	std::reverse(Ans.begin() ,Ans.end());
	for(int i=0; i<Ans.size(); ++i)
	{
		std::cout << Ans[i];
		if(i==Ans.size()-1) std::cout << std::endl;
		else std::cout << " ";
	}
	return 0;
}
0