Problem p03305 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const ll INF = INT64_MAX / 10;

struct edge {
  int to;
  ll cost1, cost2;
  edge(int x = 0, ll y = 0LL, ll z = 0LL) : to(x), cost1(y), cost2(z) {}
};

vector<vector<edge>> G;

int main() {
  int n, m, s, t;
  cin >> n >> m >> s >> t;
  --s;
  --t;
  G.resize(n);
  rep(i, m) {
    int a, b;
    ll c, d;
    cin >> a >> b >> c >> d;
    a--;
    b--;
    G[a].push_back(edge(b, c, d));
    G[b].push_back(edge(a, c, d));
  }

  priority_queue<pair<ll, int>> q;
  q.push(make_pair(0LL, s));
  vector<ll> d(n, INF);
  d[s] = 0LL;
  vector<ll> ans(n);
  while (!q.empty()) {
    auto now = q.top();
    q.pop();

    if (d[now.second] != now.first)
      continue;
    for (auto next : G[now.second]) {
      if (d[next.to] > now.first + next.cost1) {
        d[next.to] = now.first + next.cost1;
        q.push(make_pair(d[next.to], next.to));
      }
    }
  }
  vector<ll> d2(n, INF);
  d2[t] = 0;
  q.push(make_pair(0LL, t));
  while (!q.empty()) {
    auto now = q.top();
    q.pop();

    if (d2[now.second] != now.first)
      continue;
    for (auto next : G[now.second]) {
      if (d2[next.to] > now.first + next.cost2) {
        d2[next.to] = now.first + next.cost2;
        q.push(make_pair(d2[next.to], next.to));
      }
    }
  }
  ll mm = 1e15;

  rep(i, n) { ans[i] = d[i] + d2[i]; }
  rep(i, n - 1) {
    if (ans[n - 1 - i] < ans[n - 2 - i]) {
      ans[n - 2 - i] = ans[n - 1 - i];
    }
  }

  rep(i, n) { cout << mm - ans[i] << endl; }
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const ll INF = INT64_MAX / 10;

struct edge {
  int to;
  ll cost1, cost2;
  edge(int x = 0, ll y = 0LL, ll z = 0LL) : to(x), cost1(y), cost2(z) {}
};

vector<vector<edge>> G;

int main() {
  int n, m, s, t;
  cin >> n >> m >> s >> t;
  --s;
  --t;
  G.resize(n);
  rep(i, m) {
    int a, b;
    ll c, d;
    cin >> a >> b >> c >> d;
    a--;
    b--;
    G[a].push_back(edge(b, c, d));
    G[b].push_back(edge(a, c, d));
  }

  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      q;
  q.push(make_pair(0LL, s));
  vector<ll> d(n, INF);
  d[s] = 0LL;
  vector<ll> ans(n);
  while (!q.empty()) {
    auto now = q.top();
    q.pop();

    if (d[now.second] != now.first)
      continue;
    for (auto next : G[now.second]) {
      if (d[next.to] > now.first + next.cost1) {
        d[next.to] = now.first + next.cost1;
        q.push(make_pair(d[next.to], next.to));
      }
    }
  }
  vector<ll> d2(n, INF);
  d2[t] = 0;
  q.push(make_pair(0LL, t));
  while (!q.empty()) {
    auto now = q.top();
    q.pop();

    if (d2[now.second] != now.first)
      continue;
    for (auto next : G[now.second]) {
      if (d2[next.to] > now.first + next.cost2) {
        d2[next.to] = now.first + next.cost2;
        q.push(make_pair(d2[next.to], next.to));
      }
    }
  }
  ll mm = 1e15;

  rep(i, n) { ans[i] = d[i] + d2[i]; }
  rep(i, n - 1) {
    if (ans[n - 1 - i] < ans[n - 2 - i]) {
      ans[n - 2 - i] = ans[n - 1 - i];
    }
  }

  rep(i, n) { cout << mm - ans[i] << endl; }
}