Problem p00573 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define N 100005
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;

const int INF = 1e17;

int n, m, S, T, U, V;
vector<P> G[N];

int d[2][N];
P1 d2[2][N];

int dijkstra(int s, int t, int idx) {

  for (int i = 0; i < n; i++)
    d[idx][i] = INF;

  priority_queue<P, vector<P>, greater<P>> q;

  d[idx][s] = 0;
  q.push(P(0, s));

  while (!q.empty()) {

    P tmp = q.top();
    q.pop();

    int node = tmp.second;
    int cost = tmp.first;

    if (d[idx][node] < cost)
      continue;

    for (P p : G[node]) {

      int nnode = p.first;
      int ncost = cost + p.second;

      if (d[idx][nnode] > ncost) {
        d[idx][nnode] = ncost;
        q.push(P(ncost, nnode));
      }
    }
  }

  return d[idx][t];
}

void dijkstra2(int s, int t, int idx) {

  for (int i = 0; i < n; i++)
    d2[idx][i] = P1(INF, P(INF, INF));

  priority_queue<P, vector<P>, greater<P>> q;

  d2[idx][s] = P1(0, P(d[0][s], d[1][s]));

  q.push(P(0, s));

  while (!q.empty()) {

    P tmp = q.top();
    q.pop();

    int node = tmp.second;
    int cost = tmp.first;
    int u = d2[idx][node].second.first;
    int v = d2[idx][node].second.second;

    if (d2[idx][node].first < cost)
      continue;

    for (P p : G[node]) {

      int nnode = p.first;
      int ncost = cost + p.second;
      int minu = min(u, d[0][nnode]);
      int minv = min(v, d[1][nnode]);

      if (d2[idx][nnode].first > ncost) {
        d2[idx][nnode].first = ncost;
        d2[idx][nnode].second.first = minu;
        d2[idx][nnode].second.second = minv;
        q.push(P(ncost, nnode));
      }

      if (d2[idx][nnode].first == ncost) {
        d2[idx][nnode].second.first = min(d2[idx][nnode].second.first, minu);
        d2[idx][nnode].second.second = min(d2[idx][nnode].second.second, minv);
      }
    }
  }
}

int used[N];
int ans = INF;

void dfs(int x) {

  if (used[x] == 1)
    return;
  used[x] = 1;

  int minc = min(d2[0][x].second.first + d2[1][x].second.second,
                 d2[1][x].second.first + d2[0][x].second.second);

  ans = min(ans, minc);

  for (P p : G[x]) {

    int nx = p.first;
    int cost = p.second;

    if (d2[0][x].first - cost == d2[0][nx].first)
      dfs(nx);
  }
}

void solve() {

  ans = min(ans, dijkstra(U, V, 0));

  ans = min(ans, dijkstra(V, U, 1));

  dijkstra2(S, T, 0);

  dijkstra2(T, S, 1);

  dfs(T);

  cout << ans << endl;
}

signed main() {

  cin >> n >> m;

  cin >> S >> T;
  S--, T--;

  cin >> U >> V;
  U--, V--;

  for (int i = 0; i < m; i++) {

    int a, b, c;
    cin >> a >> b >> c;

    G[a - 1].push_back(P(b - 1, c));
    G[b - 1].push_back(P(a - 1, c));
  }

  solve();

  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define N 100005
using namespace std;

typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;

const int INF = 1e17;

int n, m, S, T, U, V;
vector<P> G[N];

int d[2][N];
P1 d2[2][N];

int dijkstra(int s, int t, int idx) {

  for (int i = 0; i < n; i++)
    d[idx][i] = INF;

  priority_queue<P, vector<P>, greater<P>> q;

  d[idx][s] = 0;
  q.push(P(0, s));

  while (!q.empty()) {

    P tmp = q.top();
    q.pop();

    int node = tmp.second;
    int cost = tmp.first;

    if (d[idx][node] < cost)
      continue;

    for (P p : G[node]) {

      int nnode = p.first;
      int ncost = cost + p.second;

      if (d[idx][nnode] > ncost) {
        d[idx][nnode] = ncost;
        q.push(P(ncost, nnode));
      }
    }
  }

  return d[idx][t];
}

void dijkstra2(int s, int t, int idx) {

  for (int i = 0; i < n; i++)
    d2[idx][i] = P1(INF, P(INF, INF));

  priority_queue<P2, vector<P2>, greater<P2>> q;

  d2[idx][s] = P1(0, P(d[0][s], d[1][s]));
  q.push(P2(P(0, s), P(d[0][s], d[1][s])));

  while (!q.empty()) {

    P2 tmp = q.top();
    q.pop();

    int node = tmp.first.second;
    int cost = tmp.first.first;
    int u = tmp.second.first;
    int v = tmp.second.second;

    if (d2[idx][node].first < cost)
      continue;

    for (P p : G[node]) {

      int nnode = p.first;
      int ncost = cost + p.second;
      int minu = min(u, d[0][nnode]);
      int minv = min(v, d[1][nnode]);

      if (d2[idx][nnode].first >= ncost) {
        d2[idx][nnode].first = ncost;
        d2[idx][nnode].second.first = minu;
        d2[idx][nnode].second.second = minv;
        q.push(P2(P(ncost, nnode), P(minu, minv)));
      }
    }
  }
}

int used[N];
int ans = INF;

void dfs(int x) {

  if (used[x] == 1)
    return;
  used[x] = 1;

  int minc = min(d2[0][x].second.first + d2[1][x].second.second,
                 d2[1][x].second.first + d2[0][x].second.second);

  ans = min(ans, minc);

  for (P p : G[x]) {

    int nx = p.first;
    int cost = p.second;

    if (d2[0][x].first - cost == d2[0][nx].first)
      dfs(nx);
  }
}

void solve() {

  ans = min(ans, dijkstra(U, V, 0));

  ans = min(ans, dijkstra(V, U, 1));

  dijkstra2(S, T, 0);

  dijkstra2(T, S, 1);

  dfs(T);

  cout << ans << endl;
}

signed main() {

  cin >> n >> m;

  cin >> S >> T;
  S--, T--;

  cin >> U >> V;
  U--, V--;

  for (int i = 0; i < m; i++) {

    int a, b, c;
    cin >> a >> b >> c;

    G[a - 1].push_back(P(b - 1, c));
    G[b - 1].push_back(P(a - 1, c));
  }

  solve();

  return 0;
}