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

#include <iostream>

using namespace std;

static const int TYPE_COST = 0;
static const int TYPE_TIME = 1;
static const int INF = 100000000;
static const int MAX_N = 300;
static const int MAX_M = 100;
int C[MAX_M][MAX_M];
int T[MAX_M][MAX_M];

void init(int type, int n);

int main(int argc, char **argv) {
  while (1) {
    int n, m, from, to, c, t, type;
    cin >> n;
    cin >> m;
    if (n == 0 && m == 0)
      break;
    init(TYPE_COST, m);
    init(TYPE_TIME, m);
    for (int i = 0; i < n; i++) {
      cin >> from;
      cin >> to;
      cin >> c;
      cin >> t;
      C[from - 1][to - 1] = C[to - 1][from - 1] = c;
      T[from - 1][to - 1] = T[to - 1][from - 1] = t;
    }

    for (int k = 0; k < m; k++) {
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          C[i][j] = min(C[i][j], C[i][k] + C[k][j]);
          T[i][j] = min(T[i][j], T[i][k] + T[k][j]);
        }
      }
    }

    cin >> m;
    for (int i = 0; i < m; i++) {
      cin >> from;
      cin >> to;
      cin >> type;
      if (type == TYPE_COST)
        cout << C[from - 1][to - 1] << endl;
      if (type == TYPE_TIME)
        cout << T[from - 1][to - 1] << endl;
    }
  }

  return 0;
}

void init(int type, int n) {
  if (type == TYPE_COST) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          C[i][j] = 0;
        else
          C[i][j] = INF;
      }
    }
  }

  if (type == TYPE_TIME) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          T[i][j] = 0;
        else
          T[i][j] = INF;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string.h>
#include <time.h>
#include <vector>

using namespace std;

static const int TYPE_COST = 0;
static const int TYPE_TIME = 1;
static const int INF = 100000000;

class station {
public:
  int id;
  int cost;
  bool visited;
  vector<int> edge_to;
  vector<int> edge_cost;
  vector<int> edge_time;

public:
  station(int i) : id(i), cost(INF), visited(false) {}
  void add_to(int to, int cost, int time) {
    this->edge_to.push_back(to);
    this->edge_cost.push_back(cost);
    this->edge_time.push_back(time);
  }
};

int dijkstra(vector<station>, int size, int from, int to, int type);
void reset(vector<station> sta, int size);

int main(int argc, char **argv) {
  while (1) {
    int n, m, from, to, cost, time, type;
    cin >> n;
    cin >> m;
    if (n == 0 && m == 0)
      break;

    vector<station> sta;
    for (int i = 0; i < m; i++) {
      sta.push_back(station(i));
    }

    for (int i = 0; i < n; i++) {
      cin >> from;
      cin >> to;
      cin >> cost;
      cin >> time;
      sta[from - 1].add_to(to - 1, cost, time);
      sta[to - 1].add_to(from - 1, cost, time);
    }
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> from;
      cin >> to;
      cin >> type;
      clock_t t1 = clock();
      cout << dijkstra(sta, m, from - 1, to - 1, type) << endl;
      clock_t t2 = clock();
      cout << "Time : " << t2 - t1 << " (msec)" << endl;
      reset(sta, m);
    }
  }

  return 0;
}

struct station_compare {
  bool operator()(station s1, station s2) { return s1.cost - s2.cost; }
};

int dijkstra(vector<station> sta, int size, int start, int goal, int type) {
  priority_queue<station, vector<station>, station_compare> que;
  sta[start].cost = 0;
  que.push(sta[start]);

  while (!que.empty()) {
    station s = que.top();
    que.pop();
    if (sta[s.id].cost < s.cost)
      continue;
    for (int i = 0; i < s.edge_to.size(); i++) {
      int to = s.edge_to[i];
      int cost = (type == TYPE_COST) ? s.cost + s.edge_cost[i]
                                     : s.cost + s.edge_time[i];
      if (sta[to].cost > cost) {
        sta[to].cost = cost;
        que.push(sta[to]);
      }
    }
  }

  return sta[goal].cost;
}

void reset(vector<station> sta, int size) {
  for (int i = 0; i < size; i++) {
    sta[i].visited = false;
    sta[i].cost = -1;
  }
}