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

#include <cstdio>
#include <map>
#include <queue>
#include <vector>

#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;

int main() {
  for (int n, T = 0; scanf("%d", &n), n; T++) {
    vvi adj(n), revadj(n);
    int s, g, m;
    scanf("%d%d%d", &s, &g, &m);
    s--, g--;
    map<pii, int> road;
    rep(i, m) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--, b--;
      if (a == b || road.count(mp(a, b)) > 0)
        continue;
      adj[a].pb(b);
      revadj[b].pb(a);
      road[mp(a, b)] = i + 1;
    }

    int ans, ians;
    vvi dmin(n, vi(2, INF));
    dmin[s][0] = 0;
    vvi rmin(n, vi(2, INF));
    rmin[s][0] = 0;
    priority_queue<pair<pii, int>, vector<pair<pii, int>>,
                   greater<pair<pii, int>>>
        pq;
    pq.push(mp(mp(0, 0), s));
    while (!pq.empty()) {
      pair<pii, int> a = pq.top();
      pq.pop();
      int dis = a.first.first, roadid = a.first.second, u = a.second;
      bool rev = (bool)roadid;
      if (u == g) {
        ans = dis, ians = roadid;
        break;
      }
      if (mp(dmin[u][rev], rmin[u][rev]) < mp(dis, roadid))
        continue;

      rep(i, adj[u].size()) {
        int v = adj[u][i];
        if (mp(dis + 1, roadid) < mp(dmin[v][(bool)roadid], rmin[v][rev])) {
          dmin[v][rev] = dis + 1;
          rmin[v][rev] = roadid;
          pq.push(mp(mp(dis + 1, roadid), v));
        }
      }
      if (!rev) {
        rep(i, revadj[u].size()) {
          int v = revadj[u][i], nextroadid = road[mp(v, u)];
          if (mp(dis + 1, nextroadid) < mp(dmin[v][1], rmin[v][1])) {
            dmin[v][1] = dis + 1;
            rmin[v][1] = nextroadid;
            pq.push(mp(mp(dis + 1, nextroadid), v));
          }
        }
      }
    }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <map>
#include <queue>
#include <vector>

#define mp make_pair
#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

// typedef	vector<bool>	vb;
typedef vector<int> vi;
// typedef	vector<vb>		vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;

int main() {
  for (int n; scanf("%d", &n), n;) {
    vvi adj(n), revadj(n);
    int s, g, m;
    scanf("%d%d%d", &s, &g, &m);
    s--, g--;
    map<pii, int> road;
    rep(i, m) {
      int a, b;
      scanf("%d%d", &a, &b);
      a--, b--;
      if (a == b || road.count(mp(a, b)) > 0)
        continue;
      adj[a].pb(b);
      revadj[b].pb(a);
      road[mp(a, b)] = i + 1;
    }

    int ans = INF, ians;
    vvi dmin(n, vi(2, INF));
    dmin[s][0] = 0;
    // vvb visited(n,vb(2));
    priority_queue<pair<int, pii>> pq;
    pq.push(mp(0, mp(s, 0)));
    while (!pq.empty()) {
      pair<int, pii> a = pq.top();
      pq.pop();
      int dis = -a.first, u = a.second.first, rev = a.second.second;
      if (u == g) {
        if (dis < ans || (dis == ans && rev < ians)) {
          ans = dis;
          ians = rev;
        }
        if (dis > ans)
          break;
      }
      // if(visited[u][(bool)rev])	continue;
      // visited[u][(bool)rev]=true;
      if (dmin[u][(bool)rev] < dis)
        continue;

      rep(i, adj[u].size()) {
        int v = adj[u][i];
        // if(!visited[v][(bool)rev] && dis+1<=dmin[v][(bool)rev]){
        if (dis + 1 <= dmin[v][(bool)rev]) {
          dmin[v][(bool)rev] = dis + 1;
          pq.push(mp(-dis - 1, mp(v, rev)));
        }
      }
      if (rev == 0) {
        rep(i, revadj[u].size()) {
          int v = revadj[u][i];
          // if(!visited[v][1] && dis+1<=dmin[v][1]){
          if (dis + 1 <= dmin[v][1]) {
            dmin[v][1] = dis + 1;
            pq.push(mp(-dis - 1, mp(v, road[mp(v, u)])));
          }
        }
      }
    }

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

  return 0;
}