Problem p03305 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
// first : cost ,second : index
using P = pair<long long, int>;

using graph = vector<vector<P>>;

vector<long long> dijkstra(const graph &g, int n, int start, long long inf) {
  vector<long long> dist(n, inf);
  priority_queue<P, vector<P>, greater<P>> queue;

  queue.push(make_pair(0, start));

  while (queue.size() > 0) {
    P now = queue.top();
    queue.pop();

    long long cost = now.first;
    int v = now.second;

    if (cost > dist[v])
      continue;

    dist[v] = cost;

    for (auto e : g[v]) {
      if (cost + e.first < dist[e.second]) {
        queue.push(make_pair(cost + e.first, e.second));
      }
    }
  }

  return dist;
}

int main() {
  int n, m, s, t;
  cin >> n >> m >> s >> t;
  s--;
  t--;
  vector<vector<P>> yen(n, vector<P>(0));
  vector<vector<P>> snuuk(n, vector<P>(0));
  int u, v, a, b;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> a >> b;
    u--;
    v--;
    yen[u].emplace_back(make_pair(a, v));
    yen[v].emplace_back(make_pair(a, u));
    snuuk[v].emplace_back(make_pair(b, u));
    snuuk[u].emplace_back(make_pair(b, v));
  }

  long long inf = 1'000'000'000'000'000LL;
  vector<long long> yen_val = dijkstra(yen, n, s, inf);
  vector<long long> snuuk_val = dijkstra(snuuk, n, t, inf);
  vector<pair<long long, int>> total(n);
  for (int i = 0; i < n; i++) {
    total[i] = make_pair(yen_val[i] + snuuk_val[i], i);
  }

  sort(total.begin(), total.end());
  int idx = 0;

  for (int i = 0; i < n; i++) {
    while (total[idx].second <= i - 1) {
      idx++;
    }
    cout << inf - total[idx].first << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
// first : cost ,second : index
using P = pair<long long, int>;

using graph = vector<vector<P>>;

vector<long long> dijkstra(const graph &g, int n, int start, long long inf) {
  vector<long long> dist(n, inf);
  priority_queue<P, vector<P>, greater<P>> queue;

  queue.push(make_pair(0, start));

  while (queue.size() > 0) {
    P now = queue.top();
    queue.pop();

    long long cost = now.first;
    int v = now.second;

    if (cost >= dist[v])
      continue;

    dist[v] = cost;

    for (auto e : g[v]) {
      if (cost + e.first < dist[e.second]) {
        queue.push(make_pair(cost + e.first, e.second));
      }
    }
  }

  return dist;
}

int main() {
  int n, m, s, t;
  cin >> n >> m >> s >> t;
  s--;
  t--;
  vector<vector<P>> yen(n, vector<P>(0));
  vector<vector<P>> snuuk(n, vector<P>(0));
  int u, v, a, b;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> a >> b;
    u--;
    v--;
    yen[u].emplace_back(make_pair(a, v));
    yen[v].emplace_back(make_pair(a, u));
    snuuk[v].emplace_back(make_pair(b, u));
    snuuk[u].emplace_back(make_pair(b, v));
  }

  long long inf = 1'000'000'000'000'000LL;
  vector<long long> yen_val = dijkstra(yen, n, s, inf);
  vector<long long> snuuk_val = dijkstra(snuuk, n, t, inf);
  vector<pair<long long, int>> total(n);
  for (int i = 0; i < n; i++) {
    total[i] = make_pair(yen_val[i] + snuuk_val[i], i);
  }

  sort(total.begin(), total.end());
  int idx = 0;

  for (int i = 0; i < n; i++) {
    while (total[idx].second <= i - 1) {
      idx++;
    }
    cout << inf - total[idx].first << endl;
  }

  return 0;
}