Problem p00453 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"

struct stone {
  int x;
  int y;
  int slip;
};
struct Edge {
  int to;
  int time;
};
struct aa {
  int now;
  int time;
};
class Compare {
public:
  // aa?????????????????¶
  bool operator()(const aa &l, const aa &r) { return l.time > r.time; }
};
int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    vector<stone> stos;
    for (int i = 0; i < N; ++i) {
      int k;
      cin >> k;
      for (int j = 0; j < k; ++j) {
        int x, s;
        cin >> x >> s;
        x--;
        stos.push_back(stone{x, i + 1, s});
      }
    }
    vector<vector<Edge>> es(stos.size() * (M + 1) + 2);
    for (int i = 0; i < stos.size(); ++i) {
      for (int j = 0; j < stos.size(); ++j) {
        if (i == j)
          continue;
        for (int m = 0; m <= M; ++m) {
          stone si = stos[i];
          int iid = i * (M + 1) + m;
          stone sj = stos[j];
          int jid = j * (M + 1) + m;
          if (si.y + 1 == sj.y) {
            es[iid].push_back(
                Edge{jid, abs(si.x - sj.x) * (si.slip + sj.slip)});
          } else if (si.y + 2 == sj.y && m != M) {
            es[iid].push_back(
                Edge{int(jid + 1), abs(si.x - sj.x) * (si.slip + sj.slip)});
          }
        }
      }
    }
    const int start = stos.size() * (M + 1);
    const int goal = start + 1;
    for (int i = 0; i < stos.size(); ++i) {
      stone s(stos[i]);
      for (int m = 0; m < M; ++m) {
        const int nid = i * (M + 1) + m;
        es[nid].push_back(Edge{nid + 1, 0});
        if (s.y == 2) {
          es[start].push_back(Edge{int(nid + 1), 0});
        }
        if (s.y == N - 1) {
          es[nid].push_back(Edge{goal, 0});
        }
      }
      for (int m = 0; m <= M; ++m) {
        const int nid = i * (M + 1) + m;
        if (s.y == 1) {
          es[start].push_back(Edge{nid, 0});
        }
        if (s.y == N) {
          es[nid].push_back(Edge{goal, 0});
        }
      }
    }
    priority_queue<aa, vector<aa>, Compare> que;
    que.push(aa{start, 0});
    vector<long long int> memo(es.size(), 1e18);
    memo[start] = 0;
    int ans = -1;
    while (!que.empty()) {
      aa atop(que.top());
      que.pop();
      if (atop.now == goal) {
        ans = atop.time;
        break;
      }
      for (auto e : es[atop.now]) {
        const int nexttime = atop.time + e.time;
        if (memo[e.to] > nexttime) {
          memo[e.to] = nexttime;
          que.push(aa{e.to, nexttime});
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual
///Studio 2015\Projects\programing_contest_c++\Debug\b.answer"

struct stone {
  int x;
  int y;
  int slip;
};
struct Edge {
  int to;
  int time;
};
struct aa {
  int now;
  int time;
};
class Compare {
public:
  // aa?????????????????¶
  bool operator()(const aa &l, const aa &r) { return l.time > r.time; }
};
int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (!N)
      break;
    vector<stone> stos;
    for (int i = 0; i < N; ++i) {
      int k;
      cin >> k;
      for (int j = 0; j < k; ++j) {
        int x, s;
        cin >> x >> s;
        x--;
        stos.push_back(stone{x, i + 1, s});
      }
    }
    vector<vector<Edge>> es(stos.size() * (M + 1) + 2);
    for (int i = 0; i < stos.size(); ++i) {
      for (int j = 0; j < stos.size(); ++j) {
        if (i == j)
          continue;
        for (int m = 0; m <= M; ++m) {
          stone si = stos[i];
          int iid = i * (M + 1) + m;
          stone sj = stos[j];
          int jid = j * (M + 1) + m;
          if (si.y + 1 == sj.y) {
            es[iid].push_back(
                Edge{jid, abs(si.x - sj.x) * (si.slip + sj.slip)});
          } else if (si.y + 2 == sj.y && m != M) {
            es[iid].push_back(
                Edge{int(jid + 1), abs(si.x - sj.x) * (si.slip + sj.slip)});
          }
        }
      }
    }
    const int start = stos.size() * (M + 1);
    const int goal = start + 1;
    for (int i = 0; i < stos.size(); ++i) {
      stone s(stos[i]);
      for (int m = 0; m < M; ++m) {
        const int nid = i * (M + 1) + m;
        es[nid].push_back(Edge{nid + 1, 0});
        if (s.y == 2) {
          es[start].push_back(Edge{int(nid + 1), 0});
        }
        if (s.y == N - 1) {
          es[nid].push_back(Edge{goal, 0});
        }
      }
      for (int m = 0; m <= M; ++m) {
        const int nid = i * (M + 1) + m;
        if (s.y == 1) {
          es[start].push_back(Edge{nid, 0});
        }
        if (s.y == N) {
          es[nid].push_back(Edge{goal, 0});
        }
      }
    }
    priority_queue<aa, vector<aa>, Compare> que;
    que.push(aa{start, 0});
    vector<long long int> memo(es.size(), 1e18);
    memo[start] = 0;
    int ans = -1;
    while (!que.empty()) {
      aa atop(que.top());
      que.pop();
      if (atop.now == goal) {
        ans = atop.time;
        break;
      }
      for (auto e : es[atop.now]) {
        const int nexttime = atop.time + e.time;
        if (memo[e.to] > nexttime) {
          memo[e.to] = nexttime;
          que.push(aa{e.to, nexttime});
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}