結果

問題 No.160 最短経路のうち辞書順最小
ユーザー jajagacchijajagacchi
提出日時 2017-01-07 05:09:10
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,289 bytes
コンパイル時間 944 ms
コンパイル使用メモリ 88,944 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2023-08-22 15:38:51
合計ジャッジ時間 8,456 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 8 ms
4,380 KB
testcase_06 AC 10 ms
4,384 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 232 ms
4,376 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 TLE -
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;
	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(S);
	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]==G)
		{
			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==v_vertex[current[current.size()-1]].cost+x.cost)
				{
					std::vector<int> tmp = current;
					tmp.push_back(x.to);
					st.push(tmp);
				//	std::cout << x.to <<" b" << std::endl;
//					break;
				}
			}
		}
	}
	
	std::vector<int> Ans = *std::min_element(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