Problem p00200 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
#define MAX 10000000
#define MAX_ST 101 // MAX_STATION
#define LP(i, n) for (int i = 0; i < n; i++)
class Node {
public:
  int at;
  int cost;
  Node(int a, int c) {
    at = a;
    cost = c;
  }
  bool operator<(const Node &o) const { return cost > o.cost; }
};
int feeMap[MAX_ST][MAX_ST];
int timeMap[MAX_ST][MAX_ST];
int minMap[MAX_ST];
bool visited[MAX_ST];
typedef priority_queue<Node> PQ;

int dijkstra(int st, int ed, int n, int judge) {
  PQ pq;
  pq.push(Node(st, 0));
  while (!pq.empty()) {
    Node cur = pq.top();
    pq.pop();
    if (cur.at == ed)
      return cur.cost;
    //		cout<<"now:"<<cur.at<<endl;
    for (int i = 1; i <= n; i++) {
      if ((judge && timeMap[cur.at][i] == MAX) ||
          (!judge && feeMap[cur.at][i] == MAX) || cur.at == i)
        continue;
      int cost = judge ? timeMap[cur.at][i] : feeMap[cur.at][i];
      if (minMap[i] < cur.cost + cost)
        continue;
      // cout<<"    next:"<<i<<endl;
      minMap[i] = cur.cost + cost;
      pq.push(Node(i, cur.cost + cost));
    }
  }
  cout << "not found" << endl;
  return MAX;
}

void init() {
  LP(i, MAX_ST) { LP(j, MAX_ST) feeMap[i][j] = timeMap[i][j] = MAX; }
}
void init2() {
  LP(i, MAX_ST) {
    visited[i] = false;
    minMap[i] = MAX;
  }
}
void printMap(int n) {
  LP(i, n) {
    LP(j, n) printf("%8d", feeMap[i][j] != MAX ? feeMap[i][j] : -1);
    cout << endl;
  }
  cout << "---------------------------------------------------------" << endl;
  LP(i, n) {
    LP(j, n) printf("%8d", timeMap[i][j] != MAX ? timeMap[i][j] : -1);
    cout << endl;
  }
}
int main() {
  int n, m;
  while (cin >> n >> m && (n || m)) {
    init();
    // ツ静シツ路ツ湘ョツ陛ア
    for (int a, b, cost, time; n-- && cin >> a >> b >> cost >> time;) {
      if (feeMap[a][b] > cost)
        feeMap[a][b] = feeMap[b][a] = cost;
      if (timeMap[a][b] > time)
        timeMap[a][b] = timeMap[b][a] = time;
    }
    //		printMap(m);
    int d;
    cin >> d;
    // ツ姪「ツつ「ツ債づュツつケ
    for (int p, q, r; d-- && cin >> p >> q >> r;) {
      init2();
      //			cout<<"@========== "<<p<<" to "<<q<<"
      //======="<<endl;
      cout << dijkstra(p, q, m, r) << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
#define MAX 10000000
#define MAX_ST 101 // MAX_STATION
#define LP(i, n) for (int i = 0; i < n; i++)
class Node {
public:
  int at;
  int cost;
  Node(int a, int c) {
    at = a;
    cost = c;
  }
  bool operator<(const Node &o) const { return cost > o.cost; }
};
int feeMap[MAX_ST][MAX_ST];
int timeMap[MAX_ST][MAX_ST];
int minMap[MAX_ST];
bool visited[MAX_ST];
typedef priority_queue<Node> PQ;

int dijkstra(int st, int ed, int n, int judge) {
  PQ pq;
  pq.push(Node(st, 0));
  while (!pq.empty()) {
    Node cur = pq.top();
    pq.pop();
    if (cur.at == ed)
      return cur.cost;
    //		cout<<"now:"<<cur.at<<endl;
    for (int i = 1; i <= n; i++) {
      if ((judge && timeMap[cur.at][i] == MAX) ||
          (!judge && feeMap[cur.at][i] == MAX) || cur.at == i)
        continue;
      int cost = judge ? timeMap[cur.at][i] : feeMap[cur.at][i];
      if (minMap[i] < cur.cost + cost)
        continue;
      // cout<<"    next:"<<i<<endl;
      minMap[i] = cur.cost + cost;
      pq.push(Node(i, cur.cost + cost));
    }
  }
  cout << "not found" << endl;
  return MAX;
}

void init() {
  LP(i, MAX_ST) {
    LP(j, MAX_ST) feeMap[i][j] = timeMap[i][j] = MAX;
    visited[i] = false;
    minMap[i] = MAX;
  }
}
void printMap(int n) {
  LP(i, n) {
    LP(j, n) printf("%8d", feeMap[i][j] != MAX ? feeMap[i][j] : -1);
    cout << endl;
  }
  cout << "---------------------------------------------------------" << endl;
  LP(i, n) {
    LP(j, n) printf("%8d", timeMap[i][j] != MAX ? timeMap[i][j] : -1);
    cout << endl;
  }
}
int main() {
  int n, m;
  while (cin >> n >> m && (n || m)) {
    init();
    int _n = n;
    // ツ静シツ路ツ湘ョツ陛ア
    for (int a, b, cost, time; n-- && cin >> a >> b >> cost >> time;) {
      if (feeMap[a][b] > cost)
        feeMap[a][b] = feeMap[b][a] = cost;
      if (timeMap[a][b] > time)
        timeMap[a][b] = timeMap[b][a] = time;
    }
    // printMap(_n);
    int d;
    cin >> d;
    // ツ姪「ツつ「ツ債づュツつケ
    for (int p, q, r; d-- && cin >> p >> q >> r;) {
      cout << dijkstra(p, q, _n, r) << endl;
    }
  }
  return 0;
}