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

#include <iostream>
#include <queue>
#define CMAX -1
using namespace std;

int cost[101][101];
int maxc[101];

class datum {
public:
  int cost;
  int wheres;
  datum(int c, int w) {
    cost = c;
    wheres = w;
  };
};

bool operator<(const datum &a, const datum &b) { return a.cost > b.cost; }
bool operator>(const datum &a, const datum &b) { return a.cost < b.cost; }

int main() {
  for (;;) {
    int n, k;
    int cmd;
    int x, y;
    int tcost;
    int start, end;
    cin >> n >> k;

    if (n == 0)
      break;

    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        cost[i][j] = 0;
      }
    }

    for (int i = 0; i < k; i++) {
      cin >> cmd;
      if (cmd == 1) {
        cin >> x >> y >> tcost;
        if (cost[x][y] == 0 || cost[x][y] > tcost) {
          cost[x][y] = tcost;
          cost[y][x] = tcost;
        }
      } else {
        cin >> start >> end;

        priority_queue<datum> q;
        q.push(datum(0, start));

        for (int j = 1; j <= n; j++) {
          maxc[j] = CMAX;
        }

        bool found = false;
        int finalcost = 0;

        while (!q.empty()) {
          datum now = q.top();
          q.pop();
          if (now.wheres == end) {
            found = true;
            finalcost = now.cost;
            break;
          }

          for (int j = 1; j <= n; j++) {
            if (cost[now.wheres][j] != 0) {
              if (maxc[j] == CMAX ||
                  maxc[j] > (now.cost + cost[now.wheres][j])) {
                maxc[j] = now.cost + cost[now.wheres][j];
                q.push(datum(now.cost + cost[now.wheres][j], j));
              }
            }
          }
        }
        if (found)
          cout << finalcost << endl;
        else
          cout << -1 << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#define CMAX -1
using namespace std;

int cost[101][101];
int maxc[101];

class datum {
public:
  int cost;
  int wheres;
  datum(int c, int w) {
    cost = c;
    wheres = w;
  };
};

bool operator<(const datum &a, const datum &b) { return a.cost > b.cost; }
bool operator>(const datum &a, const datum &b) { return a.cost < b.cost; }

int main() {
  for (;;) {
    int n, k;
    int cmd;
    int x, y;
    int tcost;
    int start, end;
    cin >> n >> k;
    if (n == 0)
      break;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= n; j++) {
        cost[i][j] = 0;
      }
    }

    for (int i = 0; i < k; i++) {
      cin >> cmd;
      if (cmd == 1) {
        cin >> x >> y >> tcost;
        if (cost[x][y] == 0 || cost[x][y] > tcost) {
          cost[x][y] = tcost;
          cost[y][x] = tcost;
        }
      } else {
        cin >> start >> end;

        priority_queue<datum> q;
        q.push(datum(0, start));

        for (int j = 1; j <= n; j++) {
          maxc[j] = CMAX;
        }

        bool found = false;
        int finalcost = 0;

        while (!q.empty()) {
          datum now = q.top();
          q.pop();
          if (now.wheres == end) {
            found = true;
            finalcost = now.cost;
            break;
          }

          for (int j = 1; j <= n; j++) {
            if (cost[now.wheres][j] != 0) {
              // if ( max[
              if (maxc[j] == CMAX ||
                  maxc[j] > (now.cost + cost[now.wheres][j])) {
                q.push(datum(now.cost + cost[now.wheres][j], j));
              }
            }
          }
        }

        if (found)
          cout << finalcost << endl;
        else
          cout << -1 << endl;

        /////////
      }
    }
  }
}