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

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, m) for (int(i) = (m); (i) >= 0; (i)--)
#define REPONE(i, n) FOR(i, 1, n + 1)
#define ALL(a) (a).begin(), (a).end()
#define MP make_pair

typedef pair<int, int> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1e9;
const int MOD = 1e9 + 7;

int cost[101][101];
int n, k;

signed main() {

  while (cin >> n >> k, n) {
    fill(cost[0], cost[101], INF);
    REP(i, 101) { cost[i][i] = 0; }

    bool update = false;
    REP(i, k) {
      int t, from, to;
      cin >> t;
      if (t) {
        int fare;
        cin >> from >> to >> fare;
        from--;
        to--;
        if (chmin(cost[from][to], fare))
          update = true;
        chmin(cost[to][from], fare);

        /*
                                        REP( i, n ) {
                                                REP( j, n ) {
                                                        cout << cost[i][j] << "
           ";
                                                }
                                                cout << endl;
                                        }
        */
      } else {
        if (update) {
          REP(k, n) {
            REP(i, n) {
              REP(j, n) { chmin(cost[i][j], cost[i][k] + cost[k][j]); }
            }
          }
          update = false;
        }

        cin >> from >> to;
        from--;
        to--;
        if (cost[from][to] != INF)
          cout << cost[from][to] << endl;
        else
          cout << -1 << endl;
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, m) for (int(i) = (m); (i) >= 0; (i)--)
#define REPONE(i, n) FOR(i, 1, n + 1)
#define ALL(a) (a).begin(), (a).end()
#define MP make_pair

typedef pair<int, int> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1e9;
const int MOD = 1e9 + 7;

int cost[101][101];
int n, k;

signed main() {

  while (cin >> n >> k, n) {
    fill(cost[0], cost[101], INF);
    REP(i, 101) { cost[i][i] = 0; }

    REP(i, k) {
      int t, from, to;
      cin >> t;
      if (t) {
        int fare;
        cin >> from >> to >> fare;
        from--;
        to--;
        chmin(cost[from][to], fare);
        chmin(cost[to][from], fare);
        REP(k, n) {
          REP(i, n) {
            REP(j, n) { chmin(cost[i][j], cost[i][k] + cost[k][j]); }
          }
        }
        /*
                                        REP( i, n ) {
                                                REP( j, n ) {
                                                        cout << cost[i][j] << "
           ";
                                                }
                                                cout << endl;
                                        }
                                        */
      } else {
        cin >> from >> to;
        from--;
        to--;
        if (cost[from][to] != INF)
          cout << cost[from][to] << endl;
        else
          cout << -1 << endl;
      }
    }
  }
}