結果
問題 |
No.160 最短経路のうち辞書順最小
|
ユーザー |
|
提出日時 | 2017-01-06 15:12:06 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 1,893 bytes |
コンパイル時間 | 1,090 ms |
コンパイル使用メモリ | 85,448 KB |
実行使用メモリ | 814,780 KB |
最終ジャッジ日時 | 2024-12-17 14:31:23 |
合計ジャッジ時間 | 48,166 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 8 MLE * 18 |
ソースコード
#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::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) 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; }