結果
問題 |
No.160 最短経路のうち辞書順最小
|
ユーザー |
|
提出日時 | 2016-04-05 21:51:42 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 1,917 bytes |
コンパイル時間 | 2,793 ms |
コンパイル使用メモリ | 171,356 KB |
実行使用メモリ | 813,896 KB |
最終ジャッジ日時 | 2024-10-04 02:05:42 |
合計ジャッジ時間 | 6,089 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 3 MLE * 1 -- * 22 |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long ll; struct node { int at; ll cost; //オーバーフローを避けるためlong longに int prev; node(int at, ll cost, int prev) : at(at), cost(cost), prev(prev) {} bool operator>(const node &s) const { if (cost != s.cost) return cost > s.cost; return at > s.at; } }; struct Edge { int to; ll cost; Edge(int to, ll cost) : to(to), cost(cost) {} }; typedef vector<vector<Edge> > AdjList; //隣接リスト typedef vector<Edge>::iterator Edge_it; const ll INF = 100000000000; const int NONE = 1000; AdjList graph; vector<ll> minc; //最短経路のコスト vector<int> Prev; //最短経路をたどる際の前の頂点 void dijkstra(int n, int s){ //nは頂点数、sは始点 minc = vector<ll>(n, INF); Prev = vector<int>(n, NONE); priority_queue<node, vector<node>, greater<node> > pq; pq.push(node(s, 0, NONE)); while(!pq.empty()) { node x = pq.top(); pq.pop(); if (minc[x.at] >= x.cost) { minc[x.at] = x.cost; if(Prev[x.at] > x.prev) Prev[x.at] = x.prev; } for(Edge_it i = graph[x.at].begin(), e = graph[x.at].end(); i != e; ++i) { if (minc[(*i).to] > x.cost + (*i).cost) { pq.push(node((*i).to, x.cost + (*i).cost, x.at)); } } } } int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m, s, g; cin >> n >> m >> s >> g; graph = AdjList(n); for(int i = 0; i < m; i++) { int from, to; ll cost; cin >> from >> to >> cost; graph[from].push_back(Edge(to, cost)); graph[to].push_back(Edge(from, cost)); } dijkstra(n, g); vector<int> ans; int tmp = s; while(tmp != g){ cout << tmp << " "; tmp = Prev[tmp]; } cout << g << "\n"; return 0; }