結果
問題 |
No.160 最短経路のうち辞書順最小
|
ユーザー |
![]() |
提出日時 | 2015-08-25 22:51:34 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,522 bytes |
コンパイル時間 | 639 ms |
コンパイル使用メモリ | 78,604 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-07-18 14:32:39 |
合計ジャッジ時間 | 1,398 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 8 WA * 18 |
ソースコード
#include <iostream> #include <queue> #include <vector> #include <functional> #include <string> #include <algorithm> const int max = 205; const int inf = 10000000; typedef std::pair<int, int> P; P cost[max]; std::vector<P> g[max]; int n, s, gg; void dijkstra(int k){ std::priority_queue < P, std::vector<P>, std::greater<P> > que; for (int i = 0; i < n; ++i)cost[i].first = inf, cost[i].second = inf; cost[k].first = 0; que.push(P(0, k)); while (!que.empty()){ P p = que.top(); que.pop(); int v = p.second; if (cost[v].first < p.first)continue; for (int i = 0; i < g[v].size(); ++i){ P e = g[v][i]; if (cost[e.first].first == cost[v].first + e.second){ cost[e.first].second = std::min(cost[e.first].second, v); } if (cost[e.first].first > cost[v].first + e.second){ cost[e.first].first = cost[v].first + e.second; cost[e.first].second = v; que.push(P(cost[e.first].first, e.first)); } } } } int main(){ int m; std::cin >> n >> m >> s >> gg; for (int i = 0; i < m; ++i){ int a, b, c; std::cin >> a >> b >> c; P p(b, c), q(a, c); g[a].push_back(p); g[b].push_back(q); } for (int i = 0; i <= n; ++i)std::sort(g[i].begin(), g[i].end()); dijkstra(s); std::vector<int> ans; ans.push_back(gg); while (cost[gg].second != inf){ ans.push_back(cost[gg].second); gg = cost[gg].second; } std::reverse(ans.begin(), ans.end()); std::cout << ans[0]; for (int i = 1; i < ans.size(); ++i){ std::cout << " " << ans[i]; } std::cout << std::endl; return 0; }