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

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define INF 200000000
using namespace std;
typedef pair<int, int> P;
struct line {
  int to, time, cost;
};

vector<line> L[1000];
int costv[1000];
int timev[1000];

void mincost(int from, int to) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < 1000; i++) {
    costv[i] = INF;
  }
  costv[from] = 0;
  que.push(P(0, from));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (costv[v] < p.first)
      continue;
    for (unsigned int i = 0; i < L[v].size(); i++) {
      line l = L[v][i];
      if (costv[l.to] > costv[v] + l.cost) {
        costv[l.to] = costv[v] + l.cost;
        que.push(P(costv[l.to], l.to));
      }
    }
  }
  cout << costv[to] << endl;
}

void mintime(int from, int to) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < 1000; i++) {
    timev[i] = INF;
  }
  timev[from] = 0;
  que.push(P(0, from));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (timev[v] < p.first)
      continue;
    for (unsigned int i = 0; i < L[v].size(); i++) {
      line l = L[v][i];
      if (timev[l.to] > timev[v] + l.time) {
        timev[l.to] = timev[v] + l.time;
        que.push(P(timev[l.to], l.to));
      }
    }
  }
  cout << timev[to] << endl;
}

int main() {
  int n, m, k;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < 1000; i++) {
      L[i].clear();
    }
    for (int i = 0; i < n; i++) { // input line
      line temp;
      int f, t, ti, co;
      cin >> f >> t >> co >> ti;
      temp.to = t;
      temp.time = ti;
      temp.cost = co;
      L[f].push_back(temp);
      temp.to = f;
      L[t].push_back(temp);
    }

    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      if (r == 0)
        mincost(p, q);
      else
        mintime(p, q);
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <utility>
#include <vector>
#define INF 1000000
using namespace std;

struct line {
  int from, to, time, cost;
};

vector<line> L;
int costv[100];
int timev[100];

void mincost(int from, int to) {
  for (int i = 0; i < 100; i++) {
    costv[i] = INF;
  }
  costv[from] = 0;
  while (1) {
    bool update = false;
    for (unsigned int i = 0; i < L.size(); i++) {
      line l = L[i];
      if (costv[l.from] != INF && costv[l.to] > costv[l.from] + l.cost) {
        costv[l.to] = costv[l.from] + l.cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
  cout << costv[to] << endl;
}

void mintime(int from, int to) {
  for (int i = 0; i < 100; i++) {
    timev[i] = INF;
  }
  timev[from] = 0;
  while (1) {
    bool update = false;
    for (unsigned int i = 0; i < L.size(); i++) {
      line l = L[i];
      if (timev[l.from] != INF && timev[l.to] > timev[l.from] + l.time) {
        timev[l.to] = timev[l.from] + l.time;
        update = true;
      }
    }
    if (!update)
      break;
  }
  cout << timev[to] << endl;
}

int main() {
  int n, m, k;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) { // input line
      line temp;
      int f, t, ti, co;
      cin >> f >> t >> co >> ti;
      temp.from = f;
      temp.to = t;
      temp.time = ti;
      temp.cost = co;
      L.push_back(temp);
      temp.from = t;
      temp.to = f;
      L.push_back(temp);
    }

    cin >> k;
    for (int i = 0; i < k; i++) {
      int p, q, r;
      cin >> p >> q >> r;
      if (r == 0)
        mincost(p, q);
      else
        mintime(p, q);
    }
  }
}