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

// AOJ 0526 Boat Travel
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
const int INF = 100000000;
typedef pair<int, int> P;
queue<int> a[101];
queue<int> save;
int n, k, go, boat[5000][3], st, list;
int solve() {
  int flag[101];
  queue<P> que;
  que.push(P(st, 0));
  for (int i = 0; i <= 100; i++) {
    flag[i] = INF;
  }
  while (que.size()) {
    P p = que.front();
    que.pop();
    int i = 0;
    save = a[p.first];
    while (save.size()) {
      i = save.front();
      save.pop();
      if (p.first == boat[i][0] && flag[boat[i][1]] > p.second + boat[i][2]) {
        flag[boat[i][1]] = p.second + boat[i][2];
        que.push(P(boat[i][1], p.second + boat[i][2]));
      }
      if (p.first == boat[i][1] && flag[boat[i][0]] > p.second + boat[i][2]) {
        flag[boat[i][0]] = p.second + boat[i][2];
        que.push(P(boat[i][0], p.second + boat[i][2]));
      }
    }
  }
  return flag[go];
}
int main() {
  while (cin >> n >> k) {
    int ans;
    bool jud;
    for (int i = 0; i < n; i++) {
      while (a[i].size())
        a[i].pop();
    }
    for (int i = 0; i < k; i++) {
      cin >> jud;
      if (jud == 1) {
        cin >> boat[list][0] >> boat[list][1] >> boat[list][2];
        a[boat[list][0]].push(list);
        a[boat[list][1]].push(list);
        list++;
      }
      if (jud == 0) {
        cin >> st >> go;
        ans = solve();
        if (ans == INF)
          cout << -1 << endl;
        else
          cout << ans << endl;
      }
    }
    for (int i = 0; i < 5001; i++) {
      boat[i][0] = 0;
      boat[i][1] = 0;
      boat[i][2] = 0;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

// AOJ 0526 Boat Travel
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
const int INF = 100000000;
typedef pair<int, int> P;
queue<int> a[101];
queue<int> save[101];
int n, k, go, boat[5000][4], st, list;
int solve() {
  int flag[101];
  queue<P> que;
  que.push(P(st, 0));
  for (int i = 0; i <= 100; i++) {
    flag[i] = INF;
  }
  while (que.size()) {
    P p = que.front();
    que.pop();
    int i = 0;
    save[p.first] = a[p.first];
    while (save[p.first].size()) {
      i = save[p.first].front();
      save[p.first].pop();
      if (p.first == boat[i][0] && flag[boat[i][1]] > p.second + boat[i][2]) {
        flag[boat[i][1]] = p.second + boat[i][2];
        que.push(P(boat[i][1], p.second + boat[i][2]));
      }
      if (p.first == boat[i][1] && flag[boat[i][0]] > p.second + boat[i][2]) {
        flag[boat[i][0]] = p.second + boat[i][2];
        que.push(P(boat[i][0], p.second + boat[i][2]));
      }
    }
  }
  return flag[go];
}
int main() {
  while (cin >> n >> k) {
    int ans;
    bool jud;
    list = 0;
    for (int i = 0; i < k; i++) {
      while (a[i].size())
        a[i].pop();
    }
    for (int i = 0; i < k; i++) {
      cin >> jud;
      if (jud == 1) {
        cin >> boat[list][0] >> boat[list][1] >> boat[list][2];
        a[boat[list][0]].push(list);
        a[boat[list][1]].push(list);
        list++;
      }
      if (jud == 0) {
        cin >> st >> go;
        ans = solve();
        if (ans == INF)
          cout << -1 << endl;
        else
          cout << ans << endl;
      }
    }
  }
  return 0;
}