Problem p03305 (C++)

Buggy submission (status: Time Limit Exceeded):

#include "bits/stdc++.h"
using namespace std;
#define int long long

#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) FOR(i, 0, b)
#define INF LLONG_MAX
#define all(a) (a).begin, (a).end()

int N, M, S, T;

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};
struct idxcost {
  int idx, cost;
  idxcost(int idx, int cost) : idx(idx), cost(cost) {}

  bool operator>(const idxcost &s) const { return (cost < s.cost); }
};
vector<Edge> G_en[112345];
vector<Edge> G_snu[112345];
int d[112345];
int d_snu[112345];
int ans[112345];
int pans[112345];

void dijkstra_en(int s) {
  // rep(i,112345)d[i]=0;
  priority_queue<idxcost, vector<idxcost>, greater<idxcost>> pq;
  fill(d, d + 112345, INF);
  d[s] = 0;
  pq.push(idxcost(s, 0));
  while (!pq.empty()) {
    idxcost p = pq.top();
    pq.pop();
    int v = p.idx;
    if (d[v] < p.cost)
      continue;

    for (int i = 0; i < G_en[v].size(); i++) {
      Edge e = G_en[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(idxcost(e.to, d[e.to]));
      }
    }
  }
}

void dijkstra_snu(int s) {

  priority_queue<idxcost, vector<idxcost>, greater<idxcost>> pq;
  fill(d_snu, d_snu + 112345, INF);
  d_snu[s] = 0;
  pq.push(idxcost(s, 0));
  while (!pq.empty()) {
    idxcost p = pq.top();
    pq.pop();
    int v = p.idx;
    if (d_snu[v] < p.cost)
      continue;

    for (int i = 0; i < G_snu[v].size(); i++) {
      Edge e = G_snu[v][i];
      if (d_snu[e.to] > d_snu[v] + e.cost) {
        d_snu[e.to] = d_snu[v] + e.cost;
        pq.push(idxcost(e.to, d_snu[e.to]));
      }
    }
  }
}

signed main() {

  cin >> N >> M >> S >> T;
  S--;
  T--;

  rep(i, M) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    // 0-indexedに
    u--;
    v--;
    G_en[u].push_back(Edge(v, a));
    G_en[v].push_back(Edge(u, a));
    G_snu[u].push_back(Edge(v, b));
    G_snu[v].push_back(Edge(u, b));
  }
  dijkstra_en(S);
  dijkstra_snu(T);

  rep(i, N) {
    // 両替する場所をiとした時のs->i + j->t

    int en_dis = d[i];
    int snu_dis = d_snu[i];

    ans[i] = en_dis + snu_dis;
  }

  int tans = 1000000000000000;
  pans[N] = tans;
  for (int i = N - 1; i >= 0; i--) {
    pans[i] = min(pans[i + 1], ans[i]);
  }

  rep(i, N) {
    // i番目からの都市が使える
    cout << 1000000000000000 - pans[i] << endl;
  }
}

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int long long

#define FOR(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) FOR(i, 0, b)
#define INF LLONG_MAX
#define all(a) (a).begin, (a).end()

int N, M, S, T;

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};
struct idxcost {
  int idx, cost;
  idxcost(int idx, int cost) : idx(idx), cost(cost) {}

  bool operator>(const idxcost &s) const { return (cost > s.cost); }
};
vector<Edge> G_en[112345];
vector<Edge> G_snu[112345];
int d[112345];
int d_snu[112345];
int ans[112345];
int pans[112345];

void dijkstra_en(int s) {
  // rep(i,112345)d[i]=0;
  priority_queue<idxcost, vector<idxcost>, greater<idxcost>> pq;
  fill(d, d + 112345, INF);
  d[s] = 0;
  pq.push(idxcost(s, 0));
  while (!pq.empty()) {
    idxcost p = pq.top();
    pq.pop();
    int v = p.idx;
    if (d[v] < p.cost)
      continue;

    for (int i = 0; i < G_en[v].size(); i++) {
      Edge e = G_en[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(idxcost(e.to, d[e.to]));
      }
    }
  }
}

void dijkstra_snu(int s) {

  priority_queue<idxcost, vector<idxcost>, greater<idxcost>> pq;
  fill(d_snu, d_snu + 112345, INF);
  d_snu[s] = 0;
  pq.push(idxcost(s, 0));
  while (!pq.empty()) {
    idxcost p = pq.top();
    pq.pop();
    int v = p.idx;
    if (d_snu[v] < p.cost)
      continue;

    for (int i = 0; i < G_snu[v].size(); i++) {
      Edge e = G_snu[v][i];
      if (d_snu[e.to] > d_snu[v] + e.cost) {
        d_snu[e.to] = d_snu[v] + e.cost;
        pq.push(idxcost(e.to, d_snu[e.to]));
      }
    }
  }
}

signed main() {

  cin >> N >> M >> S >> T;
  S--;
  T--;

  rep(i, M) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    // 0-indexedに
    u--;
    v--;
    G_en[u].push_back(Edge(v, a));
    G_en[v].push_back(Edge(u, a));
    G_snu[u].push_back(Edge(v, b));
    G_snu[v].push_back(Edge(u, b));
  }
  dijkstra_en(S);
  dijkstra_snu(T);

  rep(i, N) {
    // 両替する場所をiとした時のs->i + j->t

    int en_dis = d[i];
    int snu_dis = d_snu[i];

    ans[i] = en_dis + snu_dis;
  }

  int tans = 1000000000000000;
  pans[N] = tans;
  for (int i = N - 1; i >= 0; i--) {
    pans[i] = min(pans[i + 1], ans[i]);
  }

  rep(i, N) {
    // i番目からの都市が使える
    cout << 1000000000000000 - pans[i] << endl;
  }
}