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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>
using namespace std;

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

int g[1500][80];

int main() {
  int n, m;
  int k[150], x[150][10], d[150][10];

  while (scanf("%d %d", &n, &m) && (n || m)) {
    vector<P1> normal[1500];
    vector<P1> skip[1500];
    vector<int> id[150];
    int num = 0;

    for (int i = 0; i < n; i++) {
      scanf("%d", &k[i]);
      for (int j = 0; j < k[i]; j++) {
        scanf("%d %d", &x[i][j], &d[i][j]);
        id[i].push_back(num);
        num++;
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k[i]; j++) {
        if (i + 1 < n) {
          for (int z = 0; z < k[i + 1]; z++) {
            P1 tmp = P1((d[i][j] + d[i + 1][z]) * abs(x[i][j] - x[i + 1][z]),
                        id[i + 1][z]);
            normal[id[i][j]].push_back(tmp);
          }
        }
        if (i + 2 < n) {
          for (int z = 0; z < k[i + 2]; z++) {
            P1 tmp = P1((d[i][j] + d[i + 2][z]) * abs(x[i][j] - x[i + 2][z]),
                        id[i + 2][z]);
            skip[id[i][j]].push_back(tmp);
          }
        }
      }
    }

    for (int i = 0; i < num; i++)
      for (int j = 0; j <= m; j++)
        g[i][j] = 1 << 30;

    priority_queue<P2, vector<P2>, greater<P2>> q;
    for (int i = 0; i < (int)id[0].size(); i++)
      q.push(P2(P1(0, id[0][i]), 0));
    if (m > 0)
      for (int i = 0; i < (int)id[1].size(); i++)
        q.push(P2(P1(0, id[1][i]), 1));

    while (q.size()) {
      P2 p = q.top();
      q.pop();
      int risk = p.first.first, idx = p.first.second, step = p.second;

      for (int i = 0; i < (int)normal[idx].size(); i++) {
        int r = risk + normal[idx][i].first, to = normal[idx][i].second;
        if (g[to][step] > r) {
          g[to][step] = r;
          q.push(P2(P1(r, to), step));
        }
      }
      if (step < m) {
        for (int i = 0; i < (int)skip[idx].size(); i++) {
          int r = risk + skip[idx][i].first, to = skip[idx][i].second;
          if (g[to][step + 1] > r) {
            g[to][step + 1] = r;
            q.push(P2(P1(r, to), step + 1));
          }
        }
      }
    }

    int ans = 1 << 30;
    for (int i = 0; i < (int)id[n - 1].size(); i++)
      for (int j = 0; j <= m; j++)
        ans = min(ans, g[id[n - 1][i]][j]);

    for (int i = 0; i < (int)id[n - 2].size(); i++)
      for (int j = 0; j < m; j++)
        ans = min(ans, g[id[n - 2][i]][j]);

    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <vector>
using namespace std;

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

int g[1500][80];

int main() {
  int n, m;
  int k, x[1500], d[1500];
  vector<int> id[150];

  while (scanf("%d %d", &n, &m) && (n || m)) {
    vector<P1> normal[1500];
    vector<P1> skip[1500];
    int num = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d", &k);
      for (int j = 0; j < k; j++) {
        scanf("%d %d", &x[num], &d[num]);
        id[i].push_back(num);
        num++;
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (int)id[i].size(); j++) {
        if (i + 1 < n) {
          for (int z = 0; z < (int)id[i + 1].size(); z++) {
            P1 tmp = P1((d[id[i][j]] + d[id[i + 1][z]]) *
                            abs(x[id[i][j]] - x[id[i + 1][z]]),
                        id[i + 1][z]);
            normal[id[i][j]].push_back(tmp);
          }
        }
        if (i + 2 < n) {
          for (int z = 0; z < (int)id[i + 2].size(); z++) {
            P1 tmp = P1((d[id[i][j]] + d[id[i + 2][z]]) *
                            abs(x[id[i][j]] - x[id[i + 2][z]]),
                        id[i + 2][z]);
            skip[id[i][j]].push_back(tmp);
          }
        }
      }
    }

    for (int i = 0; i < num; i++)
      for (int j = 0; j <= m; j++)
        g[i][j] = 1 << 30;

    priority_queue<P2, vector<P2>, greater<P2>> q;
    for (int i = 0; i < (int)id[0].size(); i++)
      q.push(P2(P1(0, id[0][i]), 0));

    while (q.size()) {
      P2 p = q.top();
      q.pop();
      int risk = p.first.first, idx = p.first.second, step = p.second;

      for (int i = 0; i < (int)normal[idx].size(); i++) {
        int r = risk + normal[idx][i].first, to = normal[idx][i].second;
        if (g[to][step] > r) {
          g[to][step] = r;
          q.push(P2(P1(r, to), step));
        }
      }
      if (step < m) {
        for (int i = 0; i < (int)skip[idx].size(); i++) {
          int r = risk + skip[idx][i].first, to = skip[idx][i].second;
          if (g[to][step + 1] > r) {
            g[to][step + 1] = r;
            q.push(P2(P1(r, to), step + 1));
          }
        }
      }
    }

    int ans = 1 << 30;
    for (int i = 0; i < (int)id[n - 1].size(); i++)
      for (int j = 0; j <= m; j++)
        ans = min(ans, g[id[n - 1][i]][j]);

    printf("%d\\n", ans);
  }
}