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

#include <bits/stdc++.h>
#define INF (1e9)
#define N 155
#define S 85
#define M 15
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> P1;

struct Node {
  int X, cost;
  Node(){};
  Node(int a, int c) { X = a, cost = c; };
};

struct Edge {
  int y, x, cost, flag;
  Edge(){};
  Edge(int a, int b, int c, int d) { y = a, x = b, cost = c, flag = d; };
};

int n, m, k, a, b;
vector<Node> node[N];
vector<Edge> e[N][M];
int d[S][N][M];

int dijkstra(int sy, int sx, int sm) {
  priority_queue<P1, vector<P1>, greater<P1>> q;
  for (int i = 0; i < S; i++)
    for (int j = 0; j < N; j++)
      for (int l = 0; l < M; l++)
        d[i][j][l] = INF;
  d[sm][sy][sx] = 0;
  if (sm <= m)
    q.push(P1(P(0, sm), P(sy, sx)));
  while (!q.empty()) {
    P1 t = q.top();
    q.pop();
    int cost = t.first.first;
    int mcnt = t.first.second;
    int y = t.second.first;
    int x = t.second.second;
    if (d[mcnt][y][x] < cost)
      continue;
    if (n - 1 <= y || (n - 2 <= y && mcnt < m))
      return cost;
    for (int i = 0; i < e[y][x].size(); i++) {
      int ny = e[y][x][i].y, nx = e[y][x][i].x;
      int ncost = cost + e[y][x][i].cost, f = e[y][x][i].flag;
      if (f + mcnt > m)
        continue;
      if (d[f + mcnt][ny][nx] > ncost) {
        d[f + mcnt][ny][nx] = ncost;
        q.push(P1(P(ncost, f + mcnt), P(ny, nx)));
      }
    }
  }
  return INF;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < n; i++) {
      cin >> k;
      for (int j = 0; j < k; j++) {
        cin >> a >> b;
        node[i].push_back(Node(a, b));
      }
    }
    for (int i = 0; i < n; i++)
      for (int j = i + 1; j <= i + 2 && j < n; j++)
        for (int l = 0; l < node[i].size(); l++)
          for (int p = 0; p < node[j].size(); p++)
            e[i][l].push_back(Edge(j, p,
                                   (node[i][l].cost + node[j][p].cost) *
                                       abs(node[i][l].X - node[j][p].X),
                                   j - i - 1));
    int ans = INF;
    for (int i = 0; i < node[0].size(); i++)
      ans = min(ans, dijkstra(0, i, 0));
    for (int i = 0; i < node[1].size(); i++)
      ans = min(ans, dijkstra(1, i, 1));
    cout << ans << endl;
    for (int i = 0; i < N; i++)
      node[i].clear();
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++)
        e[i][j].clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF (1e9)
#define M 1005
#define N 155
#define S 85
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> P1;

struct Node {
  int x, cost;
  Node(){};
  Node(int b, int c) { x = b, cost = c; };
};

struct Edge {
  int y, x, cost, flag;
  Edge(){};
  Edge(int a, int b, int c, int d) { y = a, x = b, cost = c, flag = d; };
};

int n, m, k, a, b, X;
vector<Node> node[N];
vector<Edge> e[N][M];
int d[S][N][M];

int dijkstra(int sy, int sx, int x) {
  priority_queue<P1, vector<P1>, greater<P1>> q;
  for (int i = 0; i <= (n + 1) / 2; i++)
    for (int j = 0; j < n; j++)
      for (int l = 0; l <= X; l++)
        d[i][j][l] = INF;
  d[sy][sx][0] = 0;
  if (x <= m)
    q.push(P1(P(0, x), P(sy, sx)));
  while (!q.empty()) {
    P1 t = q.top();
    q.pop();
    int cost = t.first.first;
    int mcnt = t.first.second;
    int y = t.second.first;
    int x = t.second.second;
    if (d[mcnt][y][x] < cost)
      continue;
    for (int i = 0; i < e[y][x].size(); i++) {
      int ny = e[y][x][i].y, nx = e[y][x][i].x;
      int ncost = e[y][x][i].cost, f = e[y][x][i].flag;
      if (f + mcnt > m)
        continue;
      if (d[f + mcnt][ny][nx] > cost + ncost) {
        d[f + mcnt][ny][nx] = cost + ncost;
        q.push(P1(P(d[f + mcnt][ny][nx], f + mcnt), P(ny, nx)));
      }
    }
  }
  int res = INF;
  for (int i = 0; i < node[n - 1].size(); i++)
    for (int j = 0; j <= m; j++)
      res = min(res, d[j][n - 1][node[n - 1][i].x]);
  for (int i = 0; i < node[n - 2].size(); i++)
    for (int j = 0; j < m; j++)
      res = min(res, d[j][n - 2][node[n - 2][i].x]);
  return res;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    X = 0;
    for (int i = 0; i < n; i++) {
      cin >> k;
      for (int j = 0; j < k; j++) {
        cin >> a >> b;
        X = max(X, a - 1);
        node[i].push_back(Node(a - 1, b));
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j <= i + 2; j++) {
        if (j == n)
          break;
        for (int l = 0; l < node[i].size(); l++) {
          for (int p = 0; p < node[j].size(); p++) {
            e[i][node[i][l].x].push_back(
                Edge(j, node[j][p].x,
                     (node[i][l].cost + node[j][p].cost) *
                         abs(node[i][l].x - node[j][p].x),
                     j - i - 1));
          }
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < node[0].size(); i++)
      ans = min(ans, dijkstra(0, node[0][i].x, 0));
    for (int i = 0; i < node[1].size(); i++)
      ans = min(ans, dijkstra(0, node[0][i].x, 1));
    cout << ans << endl;
    for (int i = 0; i < N; i++)
      node[i].clear();
    for (int i = 0; i < N; i++)
      for (int j = 0; j < M; j++)
        e[i][j].clear();
  }
  return 0;
}