Problem p03305 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
template <typename X> struct Edge {
  int from;
  int to;
  X cost;

  Edge() = default;

  Edge(int from, int to, X cost = 1) : from(from), to(to), cost(cost) {}
};

template <typename X> class Graph {
private:
  int n;
  int m;
  vector<vector<Edge<X>>> edge;
  vector<vector<X>> d;
  X inf = 1e+17;

public:
  Graph(int n) : n(n) { edge.resize(n); }

  Graph(int n, int m, vector<int> a, vector<int> b, vector<X> c) : n(n), m(m) {
    edge.resize(n);
    rep(i, m) {
      add_edge(a[i], b[i], c[i]);
      add_edge(b[i], a[i], c[i]);
    }
  }

  void add_edge(int from, int to, X cost = 1) {
    edge[from].emplace_back(from, to, cost);
  }

  void dijkstra(int s) {
    d.resize(n);
    d[s].resize(n, inf);
    d[s][s] = 0;
    using pi = pair<int, X>;
    auto compare = [](const pi &a, const pi &b) -> bool {
      return a.second < b.second;
    };
    priority_queue<pi, vector<pi>, decltype(compare)> pq{compare};
    pq.emplace(s, 0);
    while (!pq.empty()) {
      pi p = pq.top();
      pq.pop();
      int v = p.first;   // 頂点
      X dist = p.second; // s->vの最短距離
      if (d[s][v] < p.second)
        continue;
      for (auto w : edge[v]) {
        if (chmin(d[s][w.to], d[s][v] + w.cost)) {
          pq.emplace(w.to, d[s][w.to]);
        }
      }
    }
  }

  X get_d(int u, int v) { return d[u][v]; }
  X get_inf() { return inf; }
};

int main() {
  int n, m, s, t;
  cin >> n >> m >> s >> t;
  s--;
  t--;
  vector<int> u(m), v(m);
  vector<ll> a(m), b(m);
  rep(i, m) {
    cin >> u[i] >> v[i] >> a[i] >> b[i];
    u[i]--;
    v[i]--;
  }

  Graph<ll> gpen(n, m, u, v, a);
  Graph<ll> gpsn(n, m, u, v, b);

  gpen.dijkstra(s);
  gpsn.dijkstra(t);

  vector<ll> res(n);
  rep(i, n) { res[i] = gpen.get_d(s, i) + gpsn.get_d(t, i); }

  ll M = 1e+15;
  ll tmp = M;
  vector<ll> ans(n);
  for (int i = n - 1; i >= 0; --i) {
    tmp = min(tmp, res[i]);
    ans[i] = M - tmp;
  }

  rep(i, n) cout << ans[i] << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
template <typename X> struct Edge {
  int from;
  int to;
  X cost;

  Edge() = default;

  Edge(int from, int to, X cost = 1) : from(from), to(to), cost(cost) {}
};

template <typename X> class Graph {
private:
  int n;
  int m;
  vector<vector<Edge<X>>> edge;
  vector<vector<X>> d;
  X inf = 1e+17;

public:
  Graph(int n) : n(n) { edge.resize(n); }

  Graph(int n, int m, vector<int> a, vector<int> b, vector<X> c) : n(n), m(m) {
    edge.resize(n);
    rep(i, m) {
      add_edge(a[i], b[i], c[i]);
      add_edge(b[i], a[i], c[i]);
    }
  }

  void add_edge(int from, int to, X cost = 1) {
    edge[from].emplace_back(from, to, cost);
  }

  void dijkstra(int s) {
    d.resize(n);
    d[s].resize(n, inf);
    d[s][s] = 0;
    using pi = pair<int, X>;
    // auto compare = [](const pi& a, const pi& b) -> bool { return a.second <
    // b.second; };
    priority_queue<pi, vector<pi>, greater<pi>> pq;
    pq.emplace(s, 0);
    while (!pq.empty()) {
      pi p = pq.top();
      pq.pop();
      int v = p.first;   // 頂点
      X dist = p.second; // s->vの最短距離
      if (d[s][v] < p.second)
        continue;
      for (auto w : edge[v]) {
        if (chmin(d[s][w.to], d[s][v] + w.cost)) {
          pq.emplace(w.to, d[s][w.to]);
        }
      }
    }
  }

  X get_d(int u, int v) { return d[u][v]; }
  X get_inf() { return inf; }
};

int main() {
  int n, m, s, t;
  cin >> n >> m >> s >> t;
  s--;
  t--;
  vector<int> u(m), v(m);
  vector<ll> a(m), b(m);
  rep(i, m) {
    cin >> u[i] >> v[i] >> a[i] >> b[i];
    u[i]--;
    v[i]--;
  }

  Graph<ll> gpen(n, m, u, v, a);
  Graph<ll> gpsn(n, m, u, v, b);

  gpen.dijkstra(s);
  gpsn.dijkstra(t);

  vector<ll> res(n);
  rep(i, n) { res[i] = gpen.get_d(s, i) + gpsn.get_d(t, i); }

  ll M = 1e+15;
  ll tmp = M;
  vector<ll> ans(n);
  for (int i = n - 1; i >= 0; --i) {
    tmp = min(tmp, res[i]);
    ans[i] = M - tmp;
  }

  rep(i, n) cout << ans[i] << "\n";

  return 0;
}