Algorithm-Library

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

View the Project on GitHub UScuber/Algorithm-Library

:heavy_check_mark: test/yosupo/Graph/Shortest-Path.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/shortest_path"

#include "../../../template/template.hpp"

#include "../../../graph/dijkstra.hpp"

int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n,m,s,t;
  cin >> n >> m >> s >> t;
  graph root(n);
  rep(i, m){
    int a,b,c;
    cin >> a >> b >> c;
    root[a].push_back({ a, b, c });
  }
  vl dist;
  vi prev;
  dijkstra<int,ll>(root, s, dist, prev);

  if(prev[t] == -1){
    cout << "-1\n";
    return 0;
  }
  int pos = t;
  vector<pii> ans;
  while(pos != s){
    ans.push_back({ prev[pos], pos });
    pos = prev[pos];
  }
  reverse(all(ans));
  const int si = ans.size();
  cout << dist[t] << " " << si << "\n";
  for(const auto &x : ans) cout << x.first << " " << x.second << "\n";
}
#line 1 "test/yosupo/Graph/Shortest-Path.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/shortest_path"

#line 1 "template/template.hpp"
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#include <cstring>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr int mod = 1000000007;
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
#line 4 "test/yosupo/Graph/Shortest-Path.test.cpp"

#line 2 "graph/template.hpp"

/**
 * @brief Graph Template
*/
template <class T>
struct Edge {
  int from,to;
  T cost;
  int idx;
  Edge(){};
  Edge(int f, int t, T c=1, int i=-1) : from(f), to(t), cost(c), idx(i){}
  Edge(int t) : to(t), from(-1), cost(1), idx(-1){}
  operator int() const{ return to; }
  bool operator<(const Edge &e){ return cost < e.cost; }
};
template <class T>
struct Graph : vector<vector<Edge<T>>> {
  Graph(){}
  Graph(const int &n) : vector<vector<Edge<T>>>(n){}
  void add_edge(int a, int b, T c=1, int i=-1){
    (*this)[a].push_back({ a, b, c, i });
  }
};
using graph = Graph<int>;
#line 2 "graph/dijkstra.hpp"

template <class T, class U = T>
void dijkstra(const Graph<T> &root, const int s, vector<U> &dist, vector<int> &prev){
  const int n = root.size();
  dist.assign(n, numeric_limits<U>::max());
  prev.assign(n, -1);
  using pui = pair<U, int>;
  priority_queue<pui, vector<pui>, greater<pui>> que;
  que.push({ 0, s });
  while(!que.empty()){
    U val; int pos;
    tie(val,pos) = que.top();
    que.pop();
    if(dist[pos] < val) continue;
    for(const auto &x : root[pos]){
      if(chmin(dist[x], val + x.cost)){
        que.push({ dist[x], x });
        prev[x] = pos;
      }
    }
  }
}
#line 6 "test/yosupo/Graph/Shortest-Path.test.cpp"

int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n,m,s,t;
  cin >> n >> m >> s >> t;
  graph root(n);
  rep(i, m){
    int a,b,c;
    cin >> a >> b >> c;
    root[a].push_back({ a, b, c });
  }
  vl dist;
  vi prev;
  dijkstra<int,ll>(root, s, dist, prev);

  if(prev[t] == -1){
    cout << "-1\n";
    return 0;
  }
  int pos = t;
  vector<pii> ans;
  while(pos != s){
    ans.push_back({ prev[pos], pos });
    pos = prev[pos];
  }
  reverse(all(ans));
  const int si = ans.size();
  cout << dist[t] << " " << si << "\n";
  for(const auto &x : ans) cout << x.first << " " << x.second << "\n";
}
Back to top page