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

/*
 * 2492.cc: goto busters
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100;
const int INF = 1 << 30;

/* typedef */

typedef queue<int> qi;
typedef pair<int, int> pii;
typedef map<string, int> msi;

/* global variables */

string lbls[MAX_N];
int gts[MAX_N], dists[MAX_N + 1];
msi lmap;

/* subroutines */

inline int next(int k) { return (gts[k] < 0) ? k + 1 : gts[k]; }

/* main */

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> lbls[i];

    if (lbls[i] == "goto") {
      gts[i] = 0;
      cin >> lbls[i];
      lbls[i].erase(lbls[i].size() - 1);
    } else {
      gts[i] = -1;
      lbls[i].erase(lbls[i].size() - 1);
      lmap[lbls[i]] = i;
    }
  }

  for (int i = 0; i < n; i++) {
    if (gts[i] >= 0)
      gts[i] = lmap[lbls[i]];
    // printf("%d: %d\\n", i, gts[i]);
  }

  for (int i = 0; i <= n; i++)
    dists[i] = INF;
  dists[0] = 0;

  priority_queue<pii> q;
  q.push(pii(0, 0));

  while (!q.empty()) {
    pii u = q.top();
    q.pop();
    int ud = -u.first, &ui = u.second;
    if (ui == n)
      break;
    if (dists[ui] != ud)
      continue;

    int vd = ud + 1;

    if (gts[ui] >= 0) {
      int vi = next(ui);
      if (dists[vi] > ud) {
        dists[vi] = ud;
        q.push(pii(-ud, vi));
      }

      vi = ui + 1;
      if (dists[vi] > vd) {
        dists[vi] = vd;
        q.push(pii(-vd, vi));
      }
    } else {
      int vi = ui + 1;
      if (dists[vi] > ud) {
        dists[vi] = ud;
        q.push(pii(-ud, vi));
      }
    }
  }

  printf("%d\\n", dists[n]);
  return 0;
}

Error: TLE

Fixed submission:

/*
 * 2492.cc: goto busters
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100;
const int INF = 1 << 30;

/* typedef */

typedef queue<int> qi;
typedef map<string, int> msi;

/* global variables */

string lbls[MAX_N];
int gts[MAX_N], dists[MAX_N + 1];
msi lmap;
bool used[MAX_N + 1];

/* subroutines */

inline int next(int k) { return (gts[k] < 0) ? k + 1 : gts[k]; }

/* main */

int main() {
  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> lbls[i];

    if (lbls[i] == "goto") {
      gts[i] = 0;
      cin >> lbls[i];
      lbls[i].erase(lbls[i].size() - 1);
    } else {
      gts[i] = -1;
      lbls[i].erase(lbls[i].size() - 1);
      lmap[lbls[i]] = i;
    }
  }

  for (int i = 0; i < n; i++) {
    if (gts[i] >= 0)
      gts[i] = lmap[lbls[i]];
    // printf("%d: %d\\n", i, gts[i]);
  }

  for (int i = 0; i <= n; i++)
    dists[i] = INF;
  dists[0] = 0;

  qi q;
  q.push(0);
  used[0] = true;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    // printf("%d: %d\\n", u, dists[u]);
    if (u == n)
      break;

    int ud = dists[u];
    int u0 = next(u);
    for (; u0 != n && !used[u0]; u0 = next(u0)) {
      // printf("  u0=%d\\n", u0);
      dists[u0] = ud, used[u0] = true;
    }
    if (u0 == n) {
      dists[u0] = ud;
      break;
    }

    for (int u1 = u0;;) {
      if (gts[u1] >= 0 && !used[u1 + 1]) {
        int v = u1 + 1;
        used[v] = true;
        dists[v] = ud + 1;
        q.push(v);
      }

      u1 = next(u1);
      if (u1 == u0)
        break;
    }
  }

  printf("%d\\n", dists[n]);
  return 0;
}