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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef pair<P, P> PP;
int n;
const int INF = 10000000;
vector<P> G[101];
int d[101][3][3];

int dijkstra() {
  priority_queue<PP, vector<PP>, greater<PP>> que;
  for (int i = 0; i <= 100; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        d[i][j][k] = INF;
      }
    }
  }
  que.push(PP(P(1, 0), P(0, 0)));
  while (que.size()) {
    PP p = que.top();
    que.pop();
    int pn = p.first.first, pc = p.first.second, pq = p.second.first,
        pu = p.second.second;
    if (pq != 1 && pn == n)
      return pc;
    if (pu && pq < 2) {
      pq++;
      for (int i = 0; i < G[pn].size(); i++) {
        int t = G[pn][i].first;
        int c = pc;
        if (d[t][pu][pq] > c) {
          d[t][pu][pq] = c;
          que.push(PP(P(t, c), P(pq, pu)));
        }
      }
      continue;
    }
    for (int i = 0; i < G[pn].size(); i++) {
      int t = G[pn][i].first;
      int c = pc + G[pn][i].second;
      if (d[t][pu][pq] > c) {
        d[t][pu][pq] = c;
        que.push(PP(P(t, c), P(pq, pu)));
      }
    }
    if (!pu) {
      pq++, pu++;
      for (int i = 0; i < G[pn].size(); i++) {
        int t = G[pn][i].first;
        int c = pc;
        if (d[t][pu][pq] > c) {
          d[t][pu][pq] = c;
          que.push(PP(P(t, c), P(pq, pu)));
        }
      }
    }
  }
  return -1;
}

int main(void) {
  int m;
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      G[a].push_back(P(b, c));
      G[b].push_back(P(a, c));
    }
    printf("%d\\n", dijkstra());
    for (int i = 0; i < 101; i++)
      G[i].clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;

typedef pair<int, int> P;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int x, y;
int ans = 100;
int fie[11][11];

void dfs(int map2[11][11], int res) {
  int map3[11][11];
  bool same[11][11];
  bool check[3];
  for (int j = 0; j < y; j++) {
    for (int i = 0; i < x; i++) {
      map3[i][j] = map2[i][j];
    }
  }
  memset(same, 0, sizeof(same));
  memset(check, 1, sizeof(check));
  queue<P> que;
  same[0][0] = 1;
  que.push(P(0, 0));
  while (que.size()) {
    P p = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      if (0 <= nx && nx < x && 0 <= ny && ny < y &&
          map2[nx][ny] == map2[0][0] && same[nx][ny] == 0) {
        que.push(P(nx, ny));
        same[nx][ny] = 1;
      }
      if (0 <= nx && nx < x && 0 <= ny && ny < y && map2[nx][ny] != map2[0][0])
        check[map2[nx][ny]] = true;
    }
  }
  bool ch = true;
  for (int j = 0; j < y; j++) {
    for (int i = 0; i < x; i++) {
      if (same[i][j] == 0)
        ch = false;
    }
  }
  check[map2[0][0]] = false;
  if (ch == true) {
    ans = min(ans, res);
    return;
  } else if (res < 20)
    for (int k = 0; k <= 2; k++) {
      if (check[k] == true) {
        for (int j = 0; j < y; j++) {
          for (int i = 0; i < x; i++) {
            if (same[i][j] == 1)
              map2[i][j] = k;
          }
        }
        dfs(map2, res + 1);
        for (int j = 0; j < y; j++) {
          for (int i = 0; i < x; i++) {
            map2[i][j] = map3[i][j];
          }
        }
      }
    }
}

int main(void) {
  string s;
  while (1) {
    scanf("%d%d", &x, &y);
    if (x == 0 && y == 0)
      break;
    ans = 100;
    for (int j = 0; j < y; j++) {
      for (int i = 0; i < x; i++) {
        cin >> s;
        if (s == "R")
          fie[i][j] = 0;
        if (s == "G")
          fie[i][j] = 1;
        if (s == "B")
          fie[i][j] = 2;
      }
    }
    dfs(fie, 0);
    printf("%d\\n", ans);
  }
  return 0;
}