Problem p03305 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
using mat = vector<vector<ll>>;

void IOS() { ios::sync_with_stdio(false), cin.tie(0); }

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

ll mod(ll a) {
  if (a > 0)
    return a % MOD;
  if (a % MOD == 0)
    return 0;
  ll x = -a / MOD + 1;
  a += x * MOD;
  return a % MOD;
}

template <typename T> void dump(T x) { cout << x << endl; }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
// int dx[4]    = {1, 0, -1, 0};
// int dy[4]    = {0, 1, 0, -1};
// ll pas[5001][5002];

vector<P> g1[114514], g2[114514];
ll d1[114514], d2[114514], unko[114514];

int main() {
  IOS();
  ll n, m, s, t;
  cin >> n >> m >> s >> t;
  ll ans = INF;
  Fill(d1, INF);
  Fill(d2, INF);
  for (int i = 0; i < m; ++i) {
    ll a, b, x, y;
    cin >> x >> y >> a >> b;
    g1[x].push_back(P(a, y));
    g1[y].push_back(P(a, x));
    g2[x].push_back(P(b, y));
    g2[y].push_back(P(b, x));
  }
  d1[s] = d2[t] = 0;
  priority_queue<P> que;
  que.push(P(0, s));
  while (!que.empty()) {
    P a = que.top();
    ll from = a.second;
    que.pop();
    for (auto i : g1[from]) {
      ll to = i.second;
      if (d1[to] > d1[from] + i.first) {
        d1[to] = d1[from] + i.first;
        que.push(P(d1[to], to));
      }
    }
  }
  que.push(P(0, t));
  while (!que.empty()) {
    P a = que.top();
    ll from = a.second;
    que.pop();
    for (auto i : g2[from]) {
      ll to = i.second;
      if (d2[to] > d2[from] + i.first) {
        d2[to] = d2[from] + i.first;
        que.push(P(d2[to], to));
      }
    }
  }

  for (int i = n; i > 0; --i) {
    ans = min(ans, d1[i] + d2[i]);
    unko[i - 1] = 1e15 - ans;
  }

  for (int i = 0; i < n; ++i) {
    dump(unko[i]);
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
using mat = vector<vector<ll>>;

void IOS() { ios::sync_with_stdio(false), cin.tie(0); }

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

ll mod(ll a) {
  if (a > 0)
    return a % MOD;
  if (a % MOD == 0)
    return 0;
  ll x = -a / MOD + 1;
  a += x * MOD;
  return a % MOD;
}

template <typename T> void dump(T x) { cout << x << endl; }
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
// int dx[4]    = {1, 0, -1, 0};
// int dy[4]    = {0, 1, 0, -1};
// ll pas[5001][5002];

vector<P> g1[114514], g2[114514];
ll d1[114514], d2[114514], unko[114514];

int main() {
  IOS();
  ll n, m, s, t;
  cin >> n >> m >> s >> t;
  ll ans = INF;
  Fill(d1, INF);
  Fill(d2, INF);
  for (int i = 0; i < m; ++i) {
    ll a, b, x, y;
    cin >> x >> y >> a >> b;
    g1[x].push_back(P(a, y));
    g1[y].push_back(P(a, x));
    g2[x].push_back(P(b, y));
    g2[y].push_back(P(b, x));
  }
  d1[s] = d2[t] = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, s));
  while (!que.empty()) {
    P a = que.top();
    ll from = a.second;
    que.pop();
    for (auto i : g1[from]) {
      ll to = i.second;
      if (d1[to] > d1[from] + i.first) {
        d1[to] = d1[from] + i.first;
        que.push(P(d1[to], to));
      }
    }
  }
  que.push(P(0, t));
  while (!que.empty()) {
    P a = que.top();
    ll from = a.second;
    que.pop();
    for (auto i : g2[from]) {
      ll to = i.second;
      if (d2[to] > d2[from] + i.first) {
        d2[to] = d2[from] + i.first;
        que.push(P(d2[to], to));
      }
    }
  }

  for (int i = n; i > 0; --i) {
    ans = min(ans, d1[i] + d2[i]);
    unko[i - 1] = 1e15 - ans;
  }

  for (int i = 0; i < n; ++i) {
    dump(unko[i]);
  }
}