結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-01-07 14:51:06 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 16 ms / 5,000 ms |
| コード長 | 2,353 bytes |
| コンパイル時間 | 1,077 ms |
| コンパイル使用メモリ | 87,272 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-12-17 16:11:01 |
| 合計ジャッジ時間 | 2,103 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#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[G].cost = 0;
std::priority_queue<Vertex , std::vector<Vertex> , std::greater<Vertex> > q;
q.push(v_vertex[G]);
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+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;
}