結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-10-21 21:03:31 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 15 ms / 5,000 ms |
| コード長 | 1,482 bytes |
| コンパイル時間 | 998 ms |
| コンパイル使用メモリ | 82,372 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-19 03:29:15 |
| 合計ジャッジ時間 | 2,327 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int INF = 1ll<<30;
struct Edge{int to; int cost;};
int n, m, s, t, u, v, a, b;
// distは予めINF埋めされているものとする
void dijkstra(int from, vector<vector<Edge>> &path, int *dist){
dist[from] = 0;
priority_queue<pair<int, int>> pq;
pq.push({dist[from], from});
while(!pq.empty()){
pair<int, int> now = pq.top(); pq.pop();
int c = -now.first;
int pos = now.second;
if(c > dist[pos]) continue;
for(Edge e : path[pos]){
if(dist[e.to] > c + e.cost){
dist[e.to] = c + e.cost;
pq.push({-dist[e.to], e.to});
}
}
}
}
int main(){
int n, m, s, g;
cin >> n >> m >> s >> g;
vector<vector<Edge>> v(n);
int a, b, c;
for(int i = 0; i < m; i++){
cin >> a >> b >> c;
v[a].push_back(Edge({b,c}));
v[b].push_back(Edge({a,c}));
}
int dist[n];
for(int i = 0; i < n; i++) dist[i] = INF;
dijkstra(g, v, dist);
vector<int> ans;
int pos = s;
ans.push_back(pos);
while(pos != g){
int next = n;
for(Edge e : v[pos]){
if(e.cost + dist[e.to] == dist[pos]) next = min(next, e.to);
}
pos = next;
ans.push_back(pos);
}
for(int i = 0; i < ans.size(); i++) cout << ans[i] << " \n"[i == ans.size()-1];
return 0;
}