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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 51;

struct data {
  int go, back, st;
  bool operator<(const data &ano) const {
    if (go == ano.go) {
      if (back == ano.back) {
        return st < ano.st;
      } else {
        return back < ano.back;
      }
    } else {
      return go < ano.go;
    }
  }
  void print() { cout << go << " " << back << " " << st << "\\n"; }
};

struct edge {
  int to, cost;
};

vector<edge> g1[MAX_N], g2[MAX_N];
int num[MAX_N];
int dist[MAX_N][MAX_N][(1 << 10)];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    map<int, int> hset;
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    rep(i, n) {
      rep(j, n) {
        rep(k, (1 << 10)) { dist[i][j][k] = INF; }
      }
    }
    rep(i, n) { g1[i].clear(), g2[i].clear(); }
    vi c(n, 0), h(n, 0);
    rep(i, n - 2) { cin >> c[i + 1] >> h[i + 1]; }
    h[n - 1] = 1000;
    rep(i, n) {
      num[i] = hset[h[i]];
      hset[h[i]]++;
    }
    rep(i, m) {
      int x, y, z;
      cin >> x >> y >> z;
      --x, --y;
      if (h[x] < h[y]) {
        g1[x].pb((edge){y, z});
      } else if (h[x] > h[y]) {
        g2[y].pb((edge){x, z});
      } else {
        g1[x].pb((edge){y, z});
        g2[y].pb((edge){x, z});
      }
    }
    queue<data> que;
    que.push((data){0, 0, 1});
    dist[0][0][1] = 0;
    while (!que.empty()) {
      data p = que.front();
      que.pop();
      // p.print();
      // show(dist[p]);
      if (h[p.go] <= h[p.back]) {
        for (auto &e : g1[p.go]) {
          int v = e.to;
          int cs = e.cost;
          data nx = p;
          nx.go = v;
          if (h[v] >= h[p.back]) {
            if (h[v] == h[p.back]) {
              bool ok = (nx.st & (1 << num[v]));
              nx.st |= (1 << num[v]);
              int nxc = dist[p.go][p.back][p.st] + cs + (ok ? 0 : c[v]);
              if (dist[nx.go][nx.back][nx.st] > nxc) {
                dist[nx.go][nx.back][nx.st] = nxc;
                que.push(nx);
              }
            } else {
              nx.st = (1 << num[v]);
              int nxc = dist[p.go][p.back][p.st] + cs + c[v];
              if (dist[nx.go][nx.back][nx.st] > nxc) {
                dist[nx.go][nx.back][nx.st] = nxc;
                que.push(nx);
              }
            }
          } else {
            int nxc = dist[p.go][p.back][p.st] + cs + c[v];
            if (dist[nx.go][nx.back][nx.st] > nxc) {
              dist[nx.go][nx.back][nx.st] = nxc;
              que.push(nx);
            }
          }
        }
      }
      if (h[p.go] >= h[p.back]) {
        for (auto &e : g2[p.back]) {
          int v = e.to;
          int cs = e.cost;
          data nx = p;
          nx.back = v;
          if (h[v] >= h[p.go]) {
            if (h[v] == h[p.go]) {
              bool ok = (nx.st & (1 << num[v]));
              nx.st |= (1 << num[v]);
              int nxc = dist[p.go][p.back][p.st] + cs + (ok ? 0 : c[v]);
              if (dist[nx.go][nx.back][nx.st] > nxc) {
                dist[nx.go][nx.back][nx.st] = nxc;
                que.push(nx);
              }
            } else {
              nx.st = (1 << num[v]);
              int nxc = dist[p.go][p.back][p.st] + cs + c[v];
              if (dist[nx.go][nx.back][nx.st] > nxc) {
                dist[nx.go][nx.back][nx.st] = nxc;
                que.push(nx);
              }
            }
          } else {
            int nxc = dist[p.go][p.back][p.st] + cs + c[v];
            if (dist[nx.go][nx.back][nx.st] > nxc) {
              dist[nx.go][nx.back][nx.st] = nxc;
              que.push(nx);
            }
          }
        }
      }
    }
    if (dist[n - 1][n - 1][1] == INF) {
      cout << "-1\\n";
    } else {
      cout << dist[n - 1][n - 1][1] << "\\n";
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto &(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 201;

struct data {
  int go, back, st;
  bool operator<(const data &ano) const {
    if (go == ano.go) {
      if (back == ano.back) {
        return st < ano.st;
      } else {
        return back < ano.back;
      }
    } else {
      return go < ano.go;
    }
  }
  void print() { cout << go << " " << back << " " << st << "\\n"; }
};

struct edge {
  int to, cost;
};

vector<edge> g1[MAX_N], g2[MAX_N];
int num[MAX_N];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    map<data, int> dist;
    map<int, int> hset;
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    rep(i, n) { g1[i].clear(), g2[i].clear(); }
    vi c(n, 0), h(n, 0);
    rep(i, n - 2) { cin >> c[i + 1] >> h[i + 1]; }
    h[n - 1] = 1000;
    rep(i, n) {
      num[i] = hset[h[i]];
      hset[h[i]]++;
    }
    rep(i, m) {
      int x, y, z;
      cin >> x >> y >> z;
      --x, --y;
      if (h[x] < h[y]) {
        g1[x].pb((edge){y, z});
      } else if (h[x] > h[y]) {
        g2[y].pb((edge){x, z});
      } else {
        g1[x].pb((edge){y, z});
        g2[y].pb((edge){x, z});
      }
    }
    queue<data> que;
    que.push((data){0, 0, 1});
    dist[(data){0, 0, 1}] = 0;
    while (!que.empty()) {
      data p = que.front();
      que.pop();
      // p.print();
      // show(dist[p]);
      if (h[p.go] <= h[p.back]) {
        for (auto &e : g1[p.go]) {
          int v = e.to;
          int cs = e.cost;
          data nx = p;
          nx.go = v;
          if (h[v] >= h[p.back]) {
            if (h[v] == h[p.back]) {
              bool ok = (nx.st & (1 << num[v]));
              nx.st |= (1 << num[v]);
              int nxc = dist[p] + cs + (ok ? 0 : c[v]);
              if (dist.find(nx) == dist.end() || dist[nx] > nxc) {
                dist[nx] = nxc;
                que.push(nx);
              }
            } else {
              nx.st = (1 << num[v]);
              int nxc = dist[p] + cs + c[v];
              if (dist.find(nx) == dist.end() || dist[nx] > nxc) {
                dist[nx] = nxc;
                que.push(nx);
              }
            }
          } else {
            int nxc = dist[p] + cs + c[v];
            if (dist.find(nx) == dist.end() || dist[nx] > nxc) {
              dist[nx] = nxc;
              que.push(nx);
            }
          }
        }
      }
      if (h[p.go] >= h[p.back]) {
        for (auto &e : g2[p.back]) {
          int v = e.to;
          int cs = e.cost;
          data nx = p;
          nx.back = v;
          if (h[v] >= h[p.go]) {
            if (h[v] == h[p.go]) {
              bool ok = (nx.st & (1 << num[v]));
              nx.st |= (1 << num[v]);
              int nxc = dist[p] + cs + (ok ? 0 : c[v]);
              if (dist.find(nx) == dist.end() || dist[nx] > nxc) {
                dist[nx] = nxc;
                que.push(nx);
              }
            } else {
              nx.st = (1 << num[v]);
              int nxc = dist[p] + cs + c[v];
              if (dist.find(nx) == dist.end() || dist[nx] > nxc) {
                dist[nx] = nxc;
                que.push(nx);
              }
            }
          } else {
            int nxc = dist[p] + cs + c[v];
            if (dist.find(nx) == dist.end() || dist[nx] > nxc) {
              dist[nx] = nxc;
              que.push(nx);
            }
          }
        }
      }
    }
    data last = (data){n - 1, n - 1, 1};
    if (dist.find(last) == dist.end()) {
      cout << "-1\\n";
    } else {
      cout << dist[last] << "\\n";
    }
  }
  return 0;
}