Problem p00453 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m;

struct edge {
  int from, to;
  int cost;
  bool sp;

  edge(int t, int c) : to(t), cost(c) {}
  edge(int t, int c, bool f) : to(t), cost(c), sp(f) {}

  bool operator<(const edge &e) const { return cost < e.cost; }
};

vector<edge> G[2000];
int d[2000][2000];

void dijkstra(int s) {
  // cost ticket place
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      que;
  rep(i, 2000) rep(j, 2000) d[i][j] = INF;
  d[s][0] = 0;
  que.push(mp(0, mp(0, s)));

  while (que.size()) {
    int c = que.top().first;
    P p = que.top().second;

    que.pop();
    int t = p.first;
    int v = p.second;

    if (d[v][t] < c)
      continue;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (e.sp) {
        if (t + 1 <= m && d[e.to][t + 1] > d[v][t] + e.cost) {
          d[e.to][t + 1] = d[v][t] + e.cost;
          que.push(mp(d[e.to][t + 1], mp(t + 1, e.to)));
        }
      } else {
        if (d[e.to][t] > d[v][t] + e.cost) {
          d[e.to][t] = d[v][t] + e.cost;
          que.push(mp(d[e.to][t], mp(t, e.to)));
        }
      }
    }
  }
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;

    rep(i, 2000) G[i].clear();

    vector<pair<P, int>> v[n];
    int id = 1;
    rep(i, n) {
      int k;
      cin >> k;

      rep(j, k) {
        int a, b;
        cin >> a >> b;
        v[i].push_back(mp(mp(a, b), id));
        id++;
      }
    }

    rep(i, v[0].size()) {
      P p = v[0][i].first;
      int a = v[0][i].second;
      G[0].push_back(edge(a, 0, false));
    }

    if (n >= 2) {
      rep(i, v[1].size()) {
        P p = v[1][i].first;
        int a = v[1][i].second;
        G[0].push_back(edge(a, 0, true));
      }
    }

    rep(i, n - 1) {
      rep(j, v[i].size()) {
        P p = v[i][j].first;
        int a = v[i][j].second;

        rep(k, v[i + 1].size()) {
          P q = v[i + 1][k].first;
          int b = v[i + 1][k].second;
          int c = (p.second + q.second) * abs(p.first - q.first);

          G[a].push_back(edge(b, c, false));
        }

        if (i >= n - 2)
          continue;

        rep(k, v[i + 2].size()) {
          P q = v[i + 2][k].first;
          int b = v[i + 2][k].second;
          int c = (p.second + q.second) * abs(p.first - q.first);

          G[a].push_back(edge(b, c, true));
        }
      }
    }

    if (n >= 2) {
      rep(i, v[n - 2].size()) {
        P p = v[n - 2][i].first;
        int b = v[n - 2][i].second;
        G[b].push_back(edge(id, 0, true));
      }
    }

    rep(i, v[n - 1].size()) {
      P p = v[n - 1][i].first;
      int b = v[n - 1][i].second;
      G[b].push_back(edge(id, 0, false));
    }

    // cout << "GRAPH" << endl;
    //
    // rep(i, id+1) {
    // 	cout << "Node:" << i << "  :";
    // 	rep(j, G[i].size()) cout << "(" << G[i][j].to << "," << G[i][j].cost <<
    // ") "; 	cout << endl;
    // }

    dijkstra(0);

    int ans = INF;
    rep(i, m + 1) { ans = min(ans, d[id][i]); }

    // rep(i, m + 1) {
    // 	cout << "ticket:" << i << " ";
    // 	cout << d[id][i] << endl;
    // }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, m;

struct edge {
  int from, to;
  int cost;
  bool sp;

  edge(int t, int c) : to(t), cost(c) {}
  edge(int t, int c, bool f) : to(t), cost(c), sp(f) {}

  bool operator<(const edge &e) const { return cost < e.cost; }
};

vector<edge> G[500];
int d[500][500];

void dijkstra(int s) {
  // cost ticket place
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      que;
  rep(i, 500) rep(j, 500) d[i][j] = INF;
  d[s][0] = 0;
  que.push(mp(0, mp(0, s)));

  while (que.size()) {
    int c = que.top().first;
    P p = que.top().second;

    que.pop();
    int t = p.first;
    int v = p.second;

    if (d[v][t] < c)
      continue;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (e.sp) {
        if (t + 1 <= m && d[e.to][t + 1] > d[v][t] + e.cost) {
          d[e.to][t + 1] = d[v][t] + e.cost;
          que.push(mp(d[e.to][t + 1], mp(t + 1, e.to)));
        }
      } else {
        if (d[e.to][t] > d[v][t] + e.cost) {
          d[e.to][t] = d[v][t] + e.cost;
          que.push(mp(d[e.to][t], mp(t, e.to)));
        }
      }
    }
  }
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;

    rep(i, 500) G[i].clear();

    vector<pair<P, int>> v[n];
    int id = 1;
    rep(i, n) {
      int k;
      cin >> k;

      rep(j, k) {
        int a, b;
        cin >> a >> b;
        v[i].push_back(mp(mp(a, b), id));
        id++;
      }
    }

    rep(i, v[0].size()) {
      P p = v[0][i].first;
      int a = v[0][i].second;
      G[0].push_back(edge(a, 0, false));
    }

    rep(i, n - 1) {
      rep(j, v[i].size()) {
        P p = v[i][j].first;
        int a = v[i][j].second;

        rep(k, v[i + 1].size()) {
          P q = v[i + 1][k].first;
          int b = v[i + 1][k].second;
          int c = (p.second + q.second) * abs(p.first - q.first);

          G[a].push_back(edge(b, c, false));
        }

        if (i >= n - 2)
          continue;

        rep(k, v[i + 2].size()) {
          P q = v[i + 2][k].first;
          int b = v[i + 2][k].second;
          int c = (p.second + q.second) * abs(p.first - q.first);

          G[a].push_back(edge(b, c, true));
        }
      }
    }

    rep(i, v[n - 1].size()) {
      P p = v[n - 1][i].first;
      int b = v[n - 1][i].second;
      G[b].push_back(edge(id, 0, false));
    }

    // cout << "GRAPH" << endl;
    //
    // rep(i, id+1) {
    // 	cout << "Node:" << i << "  :";
    // 	rep(j, G[i].size()) cout << "(" << G[i][j].to << "," << G[i][j].cost <<
    // ") "; 	cout << endl;
    // }

    dijkstra(0);

    int ans = INF;
    rep(i, m + 1) { ans = min(ans, d[id][i]); }

    // rep(i, m + 1) {
    // 	cout << "ticket:" << i << " ";
    // 	cout << d[id][i] << endl;
    // }

    cout << ans << endl;
  }
  return 0;
}