Problem p00449 (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 <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define all(c) (c).begin(), (c).end()
#define pb push_back
#define mp make_pair
#define ERASE(v, i) (v).erase(remove(all(v), i), (v).end())
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(it, c)                                                            \\
  for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define debug(x) cerr << #x << " = " << (x) << endl;
#define LINE cerr << "LINE: " << __LINE__ << endl;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> void preview(T a, T b) {
  for (T it = a; it != b; ++it)
    cerr << *it << " ";
  cerr << endl;
}

const int INF = 100000000;
const double PI = acos(-1.0), EPS = 1e-10;

int dist[101][101];

int main() {
  for (int n, k; cin >> n >> k, n || k;) {
    for (int i = 1; i <= n; i++)
      fill(dist[i], dist[i] + 101, INF);
    for (int i = 1; i <= n; i++)
      dist[i][i] = 0;
    rep(K, k) {
      int p;
      cin >> p;
      if (p == 0) {
        int a, b;
        cin >> a >> b;
        if (dist[a][b] == INF) {
          cout << -1 << endl;
        } else {
          cout << dist[a][b] << endl;
        }
      } else {
        int c, d, e;
        cin >> c >> d >> e;
        if (dist[c][d] > e) {
          dist[c][d] = e;
          dist[d][c] = e;
          for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++) {
              dist[i][j] = min(dist[i][j], dist[i][c] + dist[c][j]);
              dist[i][j] = min(dist[i][j], dist[i][d] + dist[d][j]);
              dist[j][i] = dist[i][j];
            }
        }
      }
    }
  }
  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 <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define all(c) (c).begin(), (c).end()
#define pb push_back
#define mp make_pair
#define ERASE(v, i) (v).erase(remove(all(v), i), (v).end())
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define each(it, c)                                                            \\
  for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define debug(x) cerr << #x << " = " << (x) << endl;
#define LINE cerr << "LINE: " << __LINE__ << endl;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> void preview(T a, T b) {
  for (T it = a; it != b; ++it)
    cerr << *it << " ";
  cerr << endl;
}

const int INF = 100000000;
const double PI = acos(-1.0), EPS = 1e-10;

int dist[101][101];

int main() {
  for (int n, k; cin >> n >> k, n || k;) {
    for (int i = 1; i <= n; i++)
      fill(dist[i], dist[i] + 101, INF);
    for (int i = 1; i <= n; i++)
      dist[i][i] = 0;
    rep(K, k) {
      int p;
      cin >> p;
      if (p == 0) {
        int a, b;
        cin >> a >> b;
        if (dist[a][b] == INF) {
          cout << -1 << endl;
        } else {
          cout << dist[a][b] << endl;
        }
      } else {
        int c, d, e;
        cin >> c >> d >> e;
        if (dist[c][d] > e) {
          dist[c][d] = e;
          dist[d][c] = e;
          for (int k = 1; k <= n; k++)
            for (int i = 1; i <= n; i++)
              for (int j = 1; j <= n; j++) {
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
              }
        }
      }
    }
  }
  return 0;
}