結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
Flkanjin
|
| 提出日時 | 2022-01-05 16:29:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 22 ms / 5,000 ms |
| コード長 | 3,388 bytes |
| コンパイル時間 | 1,775 ms |
| コンパイル使用メモリ | 154,520 KB |
| 最終ジャッジ日時 | 2025-01-27 08:48:41 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10L;
template<class T, class S> inline bool chmax(T &a, const S &b){
if(a < b){a = b; return true;}
return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
if(b < a){a = b; return true;}
return false;
}
template<class T, class Container> inline bool exist(Container &s, const T &e){
return (s.find(e) != std::end(s));
}
template<class T> inline bool inside(T x, T lx, T rx){
return (std::clamp(x, lx, rx) == x);
}
template<class T> inline bool inside(T x, T y, T lx, T rx, T ly, T ry){
return inside(x, lx, rx) && inside(y, ly, ry);
}
struct edge{
int to, cost;
edge(int To, int Cost): to(To), cost(Cost){}
};
void dijkstra(int s, std::vector<std::vector<edge>> &G, std::vector<long long> &d, std::vector<int> &prv){
//pair<int, int> first: 距離 second: 頂点
std::priority_queue<std::pair<long long, int>,
std::vector<std::pair<long long, int>>,
std::greater<std::pair<long long, int>>> que;
int V{int(G.size())};
d.resize(V, LINF+3);
prv.resize(V, NIL);
d[s] = 0;
que.emplace(0, s);
while(!que.empty()){
std::pair<long long, int> p{que.top()}; que.pop();
int v{p.second};
if(d[v] < p.first) continue;
for(edge &e: G[v]){
if(d[e.to] > d[v] + e.cost){
d[e.to] = d[v] + e.cost;
prv[e.to] = v;
que.emplace(d[e.to], e.to);
}
}
}
}
int main(){
int N, M, S, G; std::cin >> N >> M >> S >> G;
std::vector<std::vector<edge>> graph(N);
{
int a, b, c;
for(int i{0}; i < M; ++i){
std::cin >> a >> b >> c;
graph[a].emplace_back(b, c);
graph[b].emplace_back(a, c);
}
}
/*for(int i{0}; i < N; ++i){
std::sort(std::begin(graph[i]), std::end(graph[i]), [](edge &a, edge &b){return a.to < b.to;});
}*/
std::vector<int> prv;
std::vector<long long> d;
dijkstra(G, graph, d, prv);
std::vector<int> ans;
ans.reserve(N);
ans.push_back(S);
int cur{S};
while(cur != G){
int dist{int(d[cur])}, v{N};
for(auto [to, cost]: graph[cur]){
if(d[to] + cost == dist) chmin(v, to);
}
cur = v;
ans.push_back(v);
}
for(auto itr{std::begin(ans)}; itr != std::end(ans); ++itr){
if(itr != std::begin(ans)) std::cout << " ";
std::cout << *itr;
}
std::cout << std::endl;
return 0;
}
Flkanjin