Problem p01317 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>

#include <cstdio>

using namespace std;

#define REP(var, count) for (int var = 0; var < count; var++)

const int IPPAI = 214748364;

int umi[210][210];
int riku[210][210];

int cmpl[1010][210]; //??????????????§?????????????????????
bool used[1010][210];

bool solve(void) {
  int N, M;

  cin >> N >> M;
  if (N == 0) {
    return false;
  }

  REP(i, 210) REP(j, 210) {
    int a = (i == j) ? 0 : IPPAI;
    umi[i][j] = a;
    riku[i][j] = a;
  }

  REP(i, M) {
    int x, y, l;
    char ty;
    cin >> x >> y >> l >> ty;

    if (ty == 'L') {
      riku[x][y] = l;
      riku[y][x] = l;
    } else {
      umi[x][y] = l;
      umi[y][x] = l;
    }
  }

  // ???
  for (int k = 1; k <= N; k++) {
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= N; j++) {
        umi[i][j] = std::min(umi[i][j], umi[i][k] + umi[k][j]);
        riku[i][j] = std::min(riku[i][j], riku[i][k] + riku[k][j]);
      }
    }
  }

  int R;
  int list[1010];
  cin >> R;
  REP(i, R) { cin >> list[i]; }

  typedef pair<int, pair<int, int>> P;
  priority_queue<P, vector<P>, greater<P>> que;

  REP(i, 210) REP(f, 1010) {
    cmpl[f][i] = IPPAI;
    used[f][i] = false;
  }
  cmpl[0][list[0]] = 0;
  que.push(make_pair(0, make_pair(0, list[0])));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    pair<int, int> pa = p.second;

    int adv = pa.first;
    int nowpos = list[adv];
    int funepos = pa.second;

    if (cmpl[adv][funepos] < p.first) {
      continue;
    }
    if (adv + 1 >= R) {
      continue;
    }

    for (int i = 1; i <= N; i++) {
      // 1????????????????????????i???

      int nextpos = list[adv + 1];

      int cost;

      // cout << "adv=" << adv << ", funepos=" << funepos << ", i=" << i <<
      // endl;
      // ??????????????????
      if (funepos == i) {
        if (riku[nowpos][nextpos] == IPPAI) {
          continue;
        }
        cost = riku[nowpos][nextpos];
      } else {
        // ????????\?????´??????????????°???????????§????????§??????
        if (riku[nowpos][funepos] == IPPAI) {
          continue;
        }
        cost = riku[nowpos][funepos];

        // ?????§ i ?????§?§??????????
        if (umi[funepos][i] == IPPAI) {
          continue;
        }
        cost += umi[funepos][i];

        if (riku[i][nextpos] == IPPAI) {
          continue;
        }
        cost += riku[i][nextpos];

        cout << riku[nowpos][funepos] << " " << umi[funepos][i] << " "
             << riku[i][nextpos] << endl;
      }
      // cout << "cost = " << cost << endl;

      if (cmpl[adv + 1][i] > cmpl[adv][funepos] + cost) {
        cmpl[adv + 1][i] = cmpl[adv][funepos] + cost;
        que.push(make_pair(cmpl[adv + 1][i], make_pair(adv + 1, i)));
      }
    }
  }

  int ret = IPPAI;
  for (int i = 1; i <= N; i++) {
    ret = std::min(ret, cmpl[R - 1][i]);
  }

  cout << ret << endl;

  return true;
}

int main(void) {
  while (solve()) {
  }
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>

#include <cstdio>

using namespace std;

#define REP(var, count) for (int var = 0; var < count; var++)

const int IPPAI = 214748364;

int umi[210][210];
int riku[210][210];

int cmpl[1010][210]; //??????????????§?????????????????????
bool used[1010][210];

bool solve(void) {
  int N, M;

  cin >> N >> M;
  if (N == 0) {
    return false;
  }

  REP(i, 210) REP(j, 210) {
    int a = (i == j) ? 0 : IPPAI;
    umi[i][j] = a;
    riku[i][j] = a;
  }

  REP(i, M) {
    int x, y, l;
    char ty;
    cin >> x >> y >> l >> ty;

    if (ty == 'L') {
      riku[x][y] = l;
      riku[y][x] = l;
    } else {
      umi[x][y] = l;
      umi[y][x] = l;
    }
  }

  // ???
  for (int k = 1; k <= N; k++) {
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j <= N; j++) {
        umi[i][j] = std::min(umi[i][j], umi[i][k] + umi[k][j]);
        riku[i][j] = std::min(riku[i][j], riku[i][k] + riku[k][j]);
      }
    }
  }

  int R;
  int list[1010];
  cin >> R;
  REP(i, R) { cin >> list[i]; }

  typedef pair<int, pair<int, int>> P;
  priority_queue<P, vector<P>, greater<P>> que;

  REP(i, 210) REP(f, 1010) {
    cmpl[f][i] = IPPAI;
    used[f][i] = false;
  }
  cmpl[0][list[0]] = 0;
  que.push(make_pair(0, make_pair(0, list[0])));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    pair<int, int> pa = p.second;

    int adv = pa.first;
    int nowpos = list[adv];
    int funepos = pa.second;

    if (cmpl[adv][funepos] < p.first) {
      continue;
    }
    if (adv + 1 >= R) {
      continue;
    }

    for (int i = 1; i <= N; i++) {
      // 1????????????????????????i???

      int nextpos = list[adv + 1];

      int cost;

      // cout << "adv=" << adv << ", funepos=" << funepos << ", i=" << i <<
      // endl;
      // ??????????????????
      if (funepos == i) {
        if (riku[nowpos][nextpos] == IPPAI) {
          continue;
        }
        cost = riku[nowpos][nextpos];
      } else {
        // ????????\?????´??????????????°???????????§????????§??????
        if (riku[nowpos][funepos] == IPPAI) {
          continue;
        }
        cost = riku[nowpos][funepos];

        // ?????§ i ?????§?§??????????
        if (umi[funepos][i] == IPPAI) {
          continue;
        }
        cost += umi[funepos][i];

        if (riku[i][nextpos] == IPPAI) {
          continue;
        }
        cost += riku[i][nextpos];

        // cout << riku[nowpos][funepos] << " " << umi[funepos][i] << " " <<
        // riku[i][nextpos] << endl;
      }
      // cout << "cost = " << cost << endl;

      if (cmpl[adv + 1][i] > cmpl[adv][funepos] + cost) {
        cmpl[adv + 1][i] = cmpl[adv][funepos] + cost;
        que.push(make_pair(cmpl[adv + 1][i], make_pair(adv + 1, i)));
      }
    }
  }

  int ret = IPPAI;
  for (int i = 1; i <= N; i++) {
    ret = std::min(ret, cmpl[R - 1][i]);
  }

  cout << ret << endl;

  return true;
}

int main(void) {
  while (solve()) {
  }
}