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

#include <iostream>
using namespace std;

#define STAT_MAX 100
#define LINE_MAX 3000

int main() {

  int n;
  int p, q, r;

  int i, j;
  bool flg;

  int statNum;
  int stat[STAT_MAX];

  int lineNum;
  int lineTo[LINE_MAX][2];
  int lineCost[LINE_MAX][2];

  while (1) {
    //???????????±
    cin >> lineNum >> statNum;
    if (!lineNum && !statNum)
      break;
    for (i = 0; i < lineNum; i++) {
      cin >> lineTo[i][0] >> lineTo[i][1] >> lineCost[i][0] >> lineCost[i][1];
      lineTo[i][0]--;
      lineTo[i][1]--;
    }

    //???????????????
    cin >> n;
    while (n--) {
      cin >> p >> q >> r;
      p--;
      q--;
      for (i = 0; i < statNum; i++)
        stat[i] = 1000 * STAT_MAX;
      stat[p] = 0;

      do {
        flg = false;
        for (i = 0; i < lineNum; i++) {
          for (j = 0; j < 2; j++) {
            if (stat[lineTo[i][1 - j]] + lineCost[i][r] < stat[lineTo[i][j]]) {
              stat[lineTo[i][j]] = stat[lineTo[i][1 - j]] + lineCost[i][r];
              flg = true;
            }
          }
        }
      } while (flg);

      cout << stat[q] << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define STAT_MAX 100
#define LINE_MAX 3000

int main() {
  int n, m, a, b;
  int start, goal, flg;

  int i, j;

  int lineNum;
  int lineCost[LINE_MAX][2]; //[lineId][flg]?????????????????????????????????

  int statNum;
  int statToNum[STAT_MAX]; //[statId]??\\?¶???????????????????????????°
  int statTo
      [STAT_MAX]
      [STAT_MAX]; //[statId][i]??\\?¶??????????????????????????????????????????£????????????
  int statToId[STAT_MAX]
              [STAT_MAX]; //[statId][i]??\\?¶?????????????????????????id

  int yetNum;
  int yetId[STAT_MAX];    //?????¢?´¢???statId
  int yetDepth[STAT_MAX]; //?????¢?´¢???stat???????§???????
  int yetCost[STAT_MAX];  //?????¢?´¢???stat?????????????????????????????????
  int nowId, nowDepth, nowCost;

  int searchedNum;
  int searchedId[STAT_MAX];    //??¢?´¢?????????ID?????????
  int searchedTotal[STAT_MAX]; //?????????????´???????
  int result;

  while (1) {
    //???????????±
    cin >> n >> m;
    if (!n && !m)
      break;
    lineNum = n;
    statNum = m;
    while (m--)
      statToNum[m] = 0;
    for (i = 0; i < n; i++) {
      cin >> a >> b >> lineCost[i][0] >> lineCost[i][1];
      a--;
      b--;
      statTo[a][statToNum[a]] = b;
      statTo[b][statToNum[b]] = a;
      statToId[a][statToNum[a]] = i;
      statToId[b][statToNum[b]] = i;
      statToNum[a]++;
      statToNum[b]++;
    }
    //???????????????
    cin >> n;
    while (n--) {
      cin >> start >> goal >> flg;
      start--;
      goal--;
      result = -1;
      yetNum = 0;
      searchedNum = 0;
      yetId[yetNum] = start;
      yetDepth[yetNum] = 0;
      yetCost[yetNum] = 0;
      yetNum++;
      while (0 < yetNum) {
        //?????¢?´¢stat???????????????
        yetNum--;
        nowId = yetId[yetNum];
        nowDepth = yetDepth[yetNum];
        nowCost = yetCost[yetNum];
        //??¢?´¢???????????????
        searchedId[searchedNum] = nowId;
        searchedTotal[searchedNum] =
            searchedNum ? searchedTotal[searchedNum - 1] + nowCost : nowCost;
        searchedNum++;

        if (nowId == goal) {
          if (result == -1 || searchedTotal[searchedNum - 1] < result)
            result = searchedTotal[searchedNum - 1];
          if (yetNum != 0)
            searchedNum = yetDepth[yetNum - 1];
        } else {
          //??\\?¶?????????????????§????????´¢????????¢?´¢??????yet?????????
          m = yetNum; //??????????????????
          for (i = 0; i < statToNum[nowId]; i++) {
            for (j = 0; j < searchedNum - 1; j++) {
              if (statTo[nowId][i] == searchedId[j])
                break;
            }
            if (j != searchedNum - 1)
              continue;
            yetId[yetNum] = statTo[nowId][i];
            yetDepth[yetNum] = nowDepth + 1;
            yetCost[yetNum] = lineCost[statToId[nowId][i]][flg];
            yetNum++;
          }
          //??°?????§??????????????????
          if (m == yetNum) {
            if (yetNum != 0)
              searchedNum = yetDepth[yetNum - 1];
          }
        }
      }
      cout << result << endl;
    }
  }
  return 0;
}