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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define F(i, j, k) fill(i[0], i[0] + j * j, k)
#define P(p) cout << (p) << endl;
#define EXIST(s, e) ((s).find(e) != (s).end())
#define INF 1 << 25
#define pb push_back

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;

int main() {
  int n, k;
  int cost[101][101];
  while (cin >> n >> k, n | k) {
    fill_n((int *)cost, 101 * 101, INF);
    bool ifwf = false;
    for (int cnt = 0; cnt < k; cnt++) {
      int z;
      if (cin >> z, z == 1) {
        int c, d, e;
        cin >> c >> d >> e;
        cost[c][d] = cost[d][c] = min(cost[c][d], e);
        ifwf = true;
      } else {
        if (ifwf) {
          for (int l = 1; l <= n; l++) {
            for (int i = 1; i <= n; i++) {
              for (int j = i + 1; j <= n; j++) {
                cost[i][j] = cost[j][i] =
                    min(cost[i][j], cost[i][l] + cost[l][j]);
              }
            }
          }
          ifwf = false;
        }
        int a, b;
        cin >> a >> b;
        if (cost[a][b] != INF)
          cout << cost[a][b] << endl;
        else
          cout << -1 << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define F(i, j, k) fill(i[0], i[0] + j * j, k)
#define P(p) cout << (p) << endl;
#define EXIST(s, e) ((s).find(e) != (s).end())
#define INF 1 << 25
#define pb push_back

typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef long long ll;

int main() {
  int n, k;
  int cost[101][101];
  while (cin >> n >> k, n | k) {
    fill_n((int *)cost, 101 * 101, INF);
    bool ifwf = false;
    for (int cnt = 0; cnt < k; cnt++) {
      int z;
      if (cin >> z, z == 1) {
        int c, d, e;
        cin >> c >> d >> e;
        cost[c][d] = cost[d][c] = min(cost[c][d], e);
        ifwf = true;
      } else {
        if (ifwf) {
          for (int l = 1; l <= n; l++) {
            for (int i = 0; i <= n; i++) {
              for (int j = 1; j <= n; j++) {
                cost[i][j] = cost[j][i] =
                    min(cost[i][j], cost[i][l] + cost[l][j]);
              }
            }
          }
          ifwf = false;
        }
        int a, b;
        cin >> a >> b;
        if (cost[a][b] != INF)
          cout << cost[a][b] << endl;
        else
          cout << -1 << endl;
      }
    }
  }
  return 0;
}