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

#include <algorithm>
#include <cstdio>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

const int INF = 100000000;
const int MAX_V = 100;
int graph[MAX_V][MAX_V];
int N, k;

bool visited[MAX_V];
int cost[MAX_V];

void init_graph() {
  REP(i, N) {
    REP(j, N) {
      if (i == j)
        graph[i][j] = 0;
      else
        graph[i][j] = INF;
    }
  }
}

void make_edge(int u, int v, int c) {
  if (c < graph[u][v]) {
    graph[u][v] = graph[v][u] = c;
  }
}

// void wf() {
//     REP(k, N) {
//         REP(i, N) {
//             REP(j, N) {
//                 graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
//             }
//         }
//     }
// }

void init_dijkstra() {
  REP(i, N) visited[i] = false;
  REP(i, N) cost[i] = INF;
}

void dijkstra(int start) {
  init_dijkstra();

  cost[start] = 0;

  while (true) {
    int mi = INF;
    int nxt = -1;
    visited[start] = true;
    REP(i, N) {
      if (visited[i])
        continue;
      if (i != start) {
        int d = cost[start] + graph[start][i];
        if (d < cost[i]) {
          cost[i] = d;
        }
      }
      if (mi > cost[i]) {
        mi = cost[i];
        nxt = i;
      }
    }
    start = nxt;
    if (nxt == -1)
      break;
  }
}

void solve() {

  while (true) {
    scanf("%d %d", &N, &k);

    if (N == 0 and k == 0)
      break;

    init_graph();

    REP(_, k) {
      int mode;
      scanf("%d", &mode);
      if (mode == 0) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--;
        b--;
        dijkstra(a);
        if (cost[b] < INF) {
          printf("%d\\n", cost[b]);
        } else {
          printf("-1\\n");
        }
        // wf();
        // if (graph[a][b] < INF) {
        //     printf("%d\\n", graph[a][b]);
        // } else {
        //     printf("-1\\n");
        // }
      } else {
        int c, d, e;
        scanf("%d %d %d", &c, &d, &e);
        c--;
        d--;
        make_edge(c, d, e);
      }
    }
  }
}

int main() {
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

const int INF = 100000000;
const int MAX_V = 100;
int graph[MAX_V][MAX_V];
int N, k;

void init_graph() {
  REP(i, N) {
    REP(j, N) {
      if (i == j)
        graph[i][j] = 0;
      else
        graph[i][j] = INF;
    }
  }
}

void make_edge(int u, int v, int c) {
  if (c < graph[u][v]) {
    graph[u][v] = graph[v][u] = c;
  }
}

void wf() {
  REP(k, N) {
    REP(i, N) {
      REP(j, N) { graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]); }
    }
  }
}

void solve() {

  while (true) {
    scanf("%d %d", &N, &k);

    if (N == 0 and k == 0)
      break;

    init_graph();

    REP(_, k) {
      int mode;
      scanf("%d", &mode);
      if (mode == 0) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--;
        b--;
        wf();
        if (graph[a][b] < INF) {
          printf("%d\\n", graph[a][b]);
        } else {
          printf("-1\\n");
        }
      } else {
        int c, d, e;
        scanf("%d %d %d", &c, &d, &e);
        c--;
        d--;
        make_edge(c, d, e);
      }
    }
  }
}

int main() {
  solve();
  return 0;
}