Problem p01775 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Edge {
  int to, cost;
};
bool operator<(const Edge &e1, const Edge &e2) { return e1.cost < e2.cost; }
int N, M, Z, p, a, b, w, s[7], t[7], y[15], p1[7], p2[7], r[15], x[2][7],
    d[1111][1111];
vector<Edge> G[1111];
int main() {
  cin >> N >> M >> Z >> p;
  p--;
  r[0] = p;
  for (int i = 0; i < M; i++) {
    cin >> a >> b >> w;
    a--, b--;
    G[a].push_back(Edge{b, w});
    G[b].push_back(Edge{a, w});
  }
  for (int i = 0; i < Z; i++) {
    cin >> s[i] >> t[i];
    s[i]--, t[i]--;
  }
  y[0] = p;
  for (int i = 0; i < Z; i++)
    y[i + 1] = s[i];
  for (int i = 0; i < Z; i++)
    y[i + Z + 1] = t[i];
  for (int g = 0; g < Z * 2 + 1; g++) {
    int i = y[g];
    priority_queue<Edge> que;
    que.push(Edge{i, 0});
    fill(d[i], d[i] + N, 999999999);
    d[i][i] = 0;
    while (!que.empty()) {
      int v = que.top().to;
      que.pop();
      for (auto &e : G[v]) {
        if (d[i][e.to] > d[i][v] + e.cost) {
          d[i][e.to] = d[i][v] + e.cost;
          que.push(Edge{e.to, d[i][e.to]});
        }
      }
    }
  }
  long long ret = 999999999;
  for (int i = 0; i < (1 << (2 * Z)); i++) {
    int dep = 0;
    bool flag = true;
    for (int j = 0; j < 2 * Z; j++) {
      if (i & (1 << j))
        dep--;
      else
        dep++;
      if (dep < 0)
        flag = false;
    }
    if (dep != 0)
      flag = false;
    if (!flag)
      continue;
    for (int j = 0; j < Z; j++)
      p1[j] = p2[j] = j;
    do {
      int cr1 = 0;
      for (int j = 0; j < 2 * Z; j++) {
        if (!(i & (1 << j))) {
          x[0][p1[cr1]] = j;
          r[j + 1] = s[p1[cr1++]];
        }
      }
      do {
        int cr2 = 0;
        for (int j = 0; j < 2 * Z; j++) {
          if (i & (1 << j)) {
            x[1][p2[cr2]] = j;
            r[j + 1] = t[p2[cr2++]];
          }
        }
        bool e = true;
        for (int j = 0; j < Z; j++) {
          if (x[0][j] > x[1][j]) {
            e = false;
            break;
          }
        }
        if (!e)
          continue;
        bool ok = true;
        long long res = 0;
        for (int j = 0; j < 2 * Z; j++) {
          if (d[r[j]][r[j] + 1] == 999999999)
            ok = false;
          res += d[r[j]][r[j + 1]];
        }
        ret = min(ret, res);
      } while (next_permutation(p2, p2 + Z));
    } while (next_permutation(p1, p1 + Z));
  }
  if (ret == 999999999)
    cout << "Cannot deliver" << endl;
  else
    cout << ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Edge {
  int to, cost;
};
bool operator<(const Edge &e1, const Edge &e2) { return e1.cost < e2.cost; }
int N, M, Z, p, a, b, w, s[7], t[7], p1[7], p2[7], r[15], x[2][7],
    d[1111][1111];
vector<Edge> G[1111];
int main() {
  cin >> N >> M >> Z >> p;
  p--;
  r[0] = p;
  for (int i = 0; i < M; i++) {
    cin >> a >> b >> w;
    a--, b--;
    G[a].push_back(Edge{b, w});
    G[b].push_back(Edge{a, w});
  }
  for (int i = 0; i < Z; i++) {
    cin >> s[i] >> t[i];
    s[i]--, t[i]--;
  }
  for (int i = 0; i < N; i++) {
    priority_queue<Edge> que;
    que.push(Edge{i, 0});
    fill(d[i], d[i] + N, 999999999);
    d[i][i] = 0;
    while (!que.empty()) {
      int v = que.top().to;
      que.pop();
      for (auto &e : G[v]) {
        if (d[i][e.to] > d[i][v] + e.cost) {
          d[i][e.to] = d[i][v] + e.cost;
          que.push(Edge{e.to, d[i][e.to]});
        }
      }
    }
  }
  int ret = 999999999;
  for (int i = 0; i < (1 << (2 * Z)); i++) {
    int dep = 0;
    bool flag = true;
    for (int j = 0; j < 2 * Z; j++) {
      if (i & (1 << j))
        dep--;
      else
        dep++;
      if (dep < 0)
        flag = false;
    }
    if (dep != 0)
      flag = false;
    if (!flag)
      continue;
    for (int j = 0; j < Z; j++)
      p1[j] = p2[j] = j;
    do {
      int cr1 = 0;
      for (int j = 0; j < 2 * Z; j++) {
        if (!(i & (1 << j))) {
          x[0][p1[cr1]] = j;
          r[j + 1] = s[p1[cr1++]];
        }
      }
      do {
        int cr2 = 0;
        for (int j = 0; j < 2 * Z; j++) {
          if (i & (1 << j)) {
            x[1][p2[cr2]] = j;
            r[j + 1] = t[p2[cr2++]];
          }
        }
        bool e = true;
        for (int j = 0; j < Z; j++) {
          if (x[0][j] > x[1][j]) {
            e = false;
            break;
          }
        }
        if (!e)
          continue;
        bool ok = true;
        int res = 0;
        for (int j = 0; j < 2 * Z; j++) {
          if (d[r[j]][r[j] + 1] == 999999999)
            ok = false;
          res += d[r[j]][r[j + 1]];
        }
        ret = min(ret, res);
      } while (next_permutation(p2, p2 + Z));
    } while (next_permutation(p1, p1 + Z));
  }
  if (ret == 999999999)
    cout << "Cannot deliver" << endl;
  else
    cout << ret << endl;
  return 0;
}