Problem p00244 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
using namespace std;

#define NONE INT_MAX
enum Status { CANUSE, USING, USED };

int n, m;
double cost[100][100];

struct Data {
  int id, ticket;
  double cost;
  Data(){};
  Data(int i, double c, int t) {
    id = i;
    cost = c;
    ticket = t;
  };
};
bool operator<(Data a, Data b) {
  return !(a.cost != b.cost
               ? a.cost < b.cost
               : (a.ticket != b.ticket ? a.ticket < b.ticket : a.id < b.id));
}

int Dijkstra(int start, int goal) {
  int res = 0;
  bool used[100][3] = {0};
  priority_queue<Data> q;
  q.push(Data(0, 0.0, CANUSE));
  while (!q.empty()) {
    Data now = q.top();
    q.pop();
    used[now.id][now.ticket] = true;
    if (now.ticket != USING && now.id == goal) {
      res = now.cost;
      break;
    }
    for (int i = 0; i < n; i++) {
      if (cost[now.id][i] == NONE)
        continue;
      Data next = now;
      next.id = i;
      switch (now.ticket) {
      case CANUSE:
        next.cost += cost[now.id][next.id];
        if (!used[next.id][next.ticket])
          q.push(next);
        next.cost = now.cost;
        next.ticket = USING;
        if (!used[next.id][next.ticket])
          q.push(next);
        break;
      case USING:
        next.ticket = USED;
        if (!used[next.id][next.ticket])
          q.push(next);
        break;
      case USED:
        next.cost += cost[now.id][next.id];
        if (!used[next.id][next.ticket])
          q.push(next);
        break;
      }
    }
  }
  return res;
}

int main() {
  int a, b;
  double c;
  while (cin >> n >> m, n | m) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        cost[i][j] = NONE;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c;
      a--;
      b--;
      cost[a][b] = cost[b][a] = c;
    }
    cout << Dijkstra(0, n - 1) << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
using namespace std;

#define NONE INT_MAX
enum Status { CANUSE, USING, USED };

int n, m;
double cost[100][100];

struct Data {
  int id, ticket;
  double cost;
  Data(){};
  Data(int i, double c, int t) {
    id = i;
    cost = c;
    ticket = t;
  };
};
bool operator<(Data a, Data b) {
  return !(a.cost != b.cost
               ? a.cost < b.cost
               : (a.ticket != b.ticket ? a.ticket < b.ticket : a.id < b.id));
}

int Dijkstra(int start, int goal) {
  int res = 0;
  priority_queue<Data> q;
  q.push(Data(0, 0.0, CANUSE));
  while (!q.empty()) {
    Data now = q.top();
    q.pop();
    if (now.ticket != USING && now.id == goal) {
      res = now.cost;
      break;
    }
    for (int i = 0; i < n; i++) {
      if (cost[now.id][i] == NONE)
        continue;
      Data next = now;
      next.id = i;
      switch (now.ticket) {
      case CANUSE:
        next.cost += cost[now.id][next.id];
        q.push(next);
        next.cost = now.cost;
        next.ticket = USING;
        q.push(next);
        break;
      case USING:
        next.ticket = USED;
        q.push(next);
        break;
      case USED:
        next.cost += cost[now.id][next.id];
        q.push(next);
        break;
      }
    }
  }
  return res;
}

int main() {
  int a, b;
  double c;
  while (cin >> n >> m, n | m) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        cost[i][j] = NONE;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c;
      a--;
      b--;
      cost[a][b] = cost[b][a] = c;
    }
    cout << Dijkstra(0, n - 1) << endl;
  }
}