cpp-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub 9tc/cpp-library

:x: 単一始点最短経路(ダイクストラ法)
(graph/dijkstra-with-cost.hpp)

Verified with

Code

vector<ll> dijkstraWithCost(CostGraph& G, int s){
  int n = G.size();
  vector<ll> dist(n, LLINF);
  dist[s] = 0;
  priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> pq;
  pq.emplace(0, s);
  while(!pq.empty()) {
    auto [nowCost, now] = pq.top(); pq.pop();
    if(dist[now] < nowCost) continue;
    for(auto [to, cost]: G[now]) {
      if(chmin(dist[to], nowCost + cost)) pq.emplace(nowCost + cost, to);
    }
  }
  return dist;
}
#line 1 "graph/dijkstra-with-cost.hpp"
vector<ll> dijkstraWithCost(CostGraph& G, int s){
  int n = G.size();
  vector<ll> dist(n, LLINF);
  dist[s] = 0;
  priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> pq;
  pq.emplace(0, s);
  while(!pq.empty()) {
    auto [nowCost, now] = pq.top(); pq.pop();
    if(dist[now] < nowCost) continue;
    for(auto [to, cost]: G[now]) {
      if(chmin(dist[to], nowCost + cost)) pq.emplace(nowCost + cost, to);
    }
  }
  return dist;
}
Back to top page