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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n, k;
int cost[110][110];
int a, b;
int d[110];
bool used[110];
void dijkstra(int s) {
  for (int i = 1; i <= n; i++) {
    d[i] = INF;
    used[i] = false;
  }
  d[s] = 0;
  while (1) {
    int v = -1;
    int u;
    for (int u = 1; u <= n; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v])) {
        v = u;
      }
    }
    if (v == -1)
      break;
    used[v] = true;
    for (int u = 1; u <= n; u++) {
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }
}

int main() {
  while (cin >> n >> k, n || k) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        cost[i][j] = INF;
      }
    }
    for (int i = 0; i < k; i++) {
      int m;
      cin >> m;
      if (m == 0) {
        scanf("%d %d", &a, &b);
        dijkstra(a);
        if (d[b] != INF) {
          printf("%d\\n", d[b]);
        } else {
          printf("%d\\n", -1);
        }
      } else if (m == 1) {
        int c, d, e;
        scanf("%d %d %d", &c, &d, &e);
        cost[c][d] = cost[d][c] = min(min(cost[d][c], cost[c][d]), e);
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n, k;
int cost[110][110];
int a, b;

void wshort() {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
      }
    }
  }
}

int main() {
  while (cin >> n >> k, n || k) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        cost[i][j] = INF;
      }
    }
    for (int i = 0; i < k; i++) {
      int m;
      cin >> m;
      if (m == 0) {
        scanf("%d %d", &a, &b);
        if (cost[a][b] != INF) {
          printf("%d\\n", cost[a][b]);
        } else {
          printf("%d\\n", -1);
        }
      } else if (m == 1) {
        int c, d, e;
        scanf("%d %d %d", &c, &d, &e);
        cost[c][d] = e;
        cost[d][c] = e;
        wshort();
      }
    }
  }
  return 0;
}