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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 2;

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

    vector<vector<bool>> schedule(n, vector<bool>(31, false));
    for (int i = 0; i < n; ++i) {
      int f;
      cin >> f;
      for (int j = 0; j < f; ++j) {
        int a;
        cin >> a;
        schedule[i][a] = true;
      }
    }

    vector<vector<int>> hand(n, vector<int>(n, INF));
    for (int i = 0; i < n; ++i) {
      hand[i][i] = 0;
      multimap<int, int> mm;
      mm.insert(make_pair(0, i));
      while (!mm.empty()) {
        int day = mm.begin()->first;
        int curr = mm.begin()->second;
        mm.erase(mm.begin());
        if (day > hand[i][curr])
          continue;

        for (int j = 0; j < n; ++j) {
          for (int k = day; k <= min(30, hand[i][j] - 1); ++k) {
            if (schedule[curr][k] && schedule[j][k]) {
              hand[i][j] = k;
              mm.insert(make_pair(k, j));
            }
          }
        }
      }
    }

    int ret = INF;
    for (int i = 0; i < n; ++i) {
      int tmp = 0;
      for (int j = 0; j < n; ++j)
        tmp = max(tmp, hand[j][i]);
      ret = min(ret, tmp);
    }
    if (ret == INF)
      cout << -1 << endl;
    else
      cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const int INF = INT_MAX / 2;

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

    vector<vector<bool>> schedule(n, vector<bool>(31, false));
    for (int i = 0; i < n; ++i) {
      int f;
      cin >> f;
      for (int j = 0; j < f; ++j) {
        int a;
        cin >> a;
        schedule[i][a] = true;
      }
    }

    vector<vector<int>> meet(n, vector<int>(n, INF));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        for (int k = 30; k >= 1; --k) {
          if (schedule[i][k] && schedule[j][k])
            meet[i][j] = k;
        }
      }
    }

    vector<vector<int>> hand(n, vector<int>(n, INF));
    for (int i = 0; i < n; ++i) {
      hand[i][i] = 0;
      multimap<int, int> mm;
      mm.insert(make_pair(0, i));
      while (!mm.empty()) {
        int day = mm.begin()->first;
        int curr = mm.begin()->second;
        mm.erase(mm.begin());
        if (day > hand[i][curr])
          continue;

        for (int j = 0; j < n; ++j) {
          if (day <= meet[curr][j] && meet[curr][i] < hand[i][j]) {
            hand[i][j] = meet[curr][j];
            mm.insert(make_pair(meet[curr][j], j));
          }
        }
      }
    }

    int ret = INF;
    for (int i = 0; i < n; ++i) {
      int tmp = 0;
      for (int j = 0; j < n; ++j)
        tmp = max(tmp, hand[j][i]);
      ret = min(ret, tmp);
    }
    cout << ret << endl;
  }
}