結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
koyumeishi
|
| 提出日時 | 2015-03-02 03:05:03 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 15 ms / 5,000 ms |
| コード長 | 1,726 bytes |
| コンパイル時間 | 844 ms |
| コンパイル使用メモリ | 87,120 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-24 01:00:53 |
| 合計ジャッジ時間 | 1,855 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
typedef struct{
int to;
int cost;
}edge;
//distance from s
//O(E log V)
void dijkstra(vector< vector<edge> > &G, vector<int> &dist, vector<int> &last, int s){
//INF as distance
const int INF = 1<<29;
//first : distance from s, second : its vertex
priority_queue< pair<int, int> , vector< pair<int, int> >, greater< pair<int, int> >> pq;
fill(dist.begin(), dist.end(), INF);
dist[s] = 0;
last[s] = s;
pq.push( {dist[s], s} );
while(!pq.empty()){
auto q = pq.top();
pq.pop();
int from = q.second;
if(dist[from] < q.first) continue; // it's not minimum distance
int n=G[from].size();
for(int i=0; i<n; i++){
edge& e = G[from][i];
int next = dist[from] + e.cost;
if(dist[e.to] == next){
last[e.to] = min(last[e.to], from);
}else if(dist[e.to] > next){
dist[e.to] = next;
last[e.to] = from;
pq.push( {dist[e.to], e.to} );
}
}
}
}
void add_edge(vector<vector<edge> > &G, int from, int to, int cost){
G[from].push_back( (edge){to, cost} );
G[to].push_back( (edge){from, cost} );
}
int main(){
int N,M,S,G;
cin >> N >> M >> S >> G;
vector<vector<edge>> Graph(N);
for(int i=0; i<M; i++){
int a,b,c;
cin >> a >> b >> c;
add_edge(Graph, a, b, c);
}
vector<int> dist(N);
vector<int> last(N, -1);
dijkstra(Graph, dist, last, G);
vector<int> ans;
{
for(int pos = S; pos != last[pos]; pos = last[pos]){
ans.push_back(pos);
}
ans.push_back(G);
}
for(int i=0; i<ans.size(); i++){
cout << ans[i] << " ";
}
cout << endl;
return 0;
}
koyumeishi