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

#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 <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 1000;
const int MAX_M = 10000;

const int INF = 1 << 30;

/* typedef */

typedef pair<int, int> pii;

/* global variables */

int n, m;
int st, gl;
pii rds[MAX_M];
int rmtx[MAX_N][MAX_N], dmtx[MAX_N][MAX_N];
int dists[MAX_N];

/* subroutines */

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

  queue<int> q;
  q.push(st);

  while (!q.empty()) {
    int u = q.front();
    q.pop();

    if (u == gl)
      break;
    int nvd = dists[u] + 1;

    for (int v = 0; v < n; v++)
      if (rmtx[u][v] && dists[v] >= INF) {
        dists[v] = nvd;
        q.push(v);
      }
  }

  return dists[gl];
}

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      break;

    cin >> st >> gl;
    st--, gl--;

    cin >> m;

    memset(rmtx, 0, sizeof(rmtx));

    for (int i = 0; i < m; i++) {
      int ai, bi;
      cin >> ai >> bi;
      ai--, bi--;
      rds[i] = pii(ai, bi);

      if (ai != bi)
        rmtx[ai][bi]++;
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        dmtx[i][j] = INF;
      dmtx[i][i] = 0;
      queue<int> q;
      q.push(i);

      while (!q.empty()) {
        int u = q.front();
        q.pop();
        int nvd = dmtx[i][u] + 1;
        for (int v = 0; v < n; v++)
          if (rmtx[u][v] && dmtx[i][v] >= INF) {
            dmtx[i][v] = nvd;
            q.push(v);
          }
      }
    }

    int min_c = min_count();
    int min_ri = -1;
    // cout << min_c << endl;

    for (int ri = 0; ri < m; ri++) {
      int ai = rds[ri].first;
      int bi = rds[ri].second;
      if (rmtx[ai][bi] == 0)
        continue;

      if (min_c > dmtx[st][bi] + 1 + dmtx[ai][gl]) {
        rmtx[ai][bi]--;
        rmtx[bi][ai]++;

        int c = min_count();
        if (min_c > c) {
          min_c = c;
          min_ri = ri;
        }

        rmtx[bi][ai]--;
        rmtx[ai][bi]++;
      }
    }

    printf("%d %d\\n", min_c, min_ri + 1);
  }

  return 0;
}

Error: TLE

Fixed submission:

#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 <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 1000;
const int MAX_M = 10000;

const int INF = 1 << 30;

/* typedef */

typedef pair<int, int> pii;
typedef vector<pii> vpii;

/* global variables */

int n, m;
int st, gl;
vpii rds;
bool rmtx[MAX_N][MAX_N];
int dists[MAX_N];

/* subroutines */

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

  queue<int> q;
  q.push(st);

  while (!q.empty()) {
    int u = q.front();
    q.pop();

    if (u == gl)
      break;
    int nvd = dists[u] + 1;

    for (int v = 0; v < n; v++)
      if (rmtx[u][v] && dists[v] >= INF) {
        dists[v] = nvd;
        q.push(v);
      }
  }

  return dists[gl];
}

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      break;

    cin >> st >> gl;
    st--, gl--;

    cin >> m;

    memset(rmtx, 0, sizeof(rmtx));
    rds.clear();

    for (int i = 0; i < m; i++) {
      int ai, bi;
      cin >> ai >> bi;
      ai--, bi--;

      if (ai != bi && !rmtx[ai][bi]) {
        rmtx[ai][bi] = true;
        rds.push_back(pii(ai, bi));
      }
    }

    int rn = rds.size();
    int min_c = min_count();
    int min_ri = -1;
    // cout << min_c << endl;

    for (int ri = 0; ri < rn; ri++) {
      int ai = rds[ri].first;
      int bi = rds[ri].second;

      rmtx[ai][bi] = false;
      rmtx[bi][ai] = true;

      int c = min_count();
      if (min_c > c) {
        min_c = c;
        min_ri = ri;
      }

      rmtx[bi][ai] = false;
      rmtx[ai][bi] = true;
    }

    printf("%d %d\\n", min_c, min_ri + 1);
  }

  return 0;
}