結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-04-05 22:41:50 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 10 ms / 5,000 ms |
| コード長 | 1,893 bytes |
| コンパイル時間 | 3,198 ms |
| コンパイル使用メモリ | 170,780 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-10-04 02:06:05 |
| 合計ジャッジ時間 | 2,835 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#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;
if (prev != s.prev) return prev > s.prev;
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] != INF) continue;
minc[x.at] = x.cost;
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] == INF) {
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);
int tmp = s;
while(tmp != g){
cout << tmp << " ";
tmp = Prev[tmp];
}
cout << g << "\n";
return 0;
}