Problem p00750 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i
?????????????????????????????? ???  |???|
????????? /?????? /??£??£??£??£/??????|
??? ???_(__??????/???    ???/ .| .|????????????
??? ????????????/????????????/??????u??????
---------------------------------------------------------------------------------------------------*/

#define INF "#"
int N, A, S, G, X[401], Y[401];
string LAB[401];
string ans[401];
//---------------------------------------------------------------------------------------------------
void sol() {
  rep(i, 0, A) cin >> X[i] >> Y[i] >> LAB[i];

  vector<string> v;
  rep(i, 0, A) v.push_back(LAB[i]);

  rep(i, 0, N) ans[i] = INF;
  ans[G] = "";

  rep(i, 0, N * 6) rep(j, 0, A) {
    int a = X[j], b = Y[j];
    if (ans[b] == INF)
      continue;
    if (ans[a] == INF)
      ans[a] = LAB[j] + ans[b];
    else {
      string aa = ans[a], bb = LAB[j] + ans[b];
      if (bb < aa) {
        ans[a] = bb;
      }
    }
  }

  string pre = ans[S];

  rep(i, 0, N * 6) rep(j, 0, A) {
    int a = X[j], b = Y[j];
    if (ans[b] == INF)
      continue;
    if (ans[a] == INF)
      ans[a] = LAB[j] + ans[b];
    else {
      string aa = ans[a], bb = LAB[j] + ans[b];
      if (bb < aa) {
        ans[a] = bb;
      }
    }
  }

  if (ans[S] < pre || ans[S] == INF) {
    printf("NO\n");
    return;
  }

  printf("%s\n", ans[S].c_str());
}
//---------------------------------------------------------------------------------------------------
void _main() {
  int t = 1;
  while (cin >> N >> A >> S >> G) {
    if (N == 0)
      return;
    sol();
    fprintf(stderr, "Finish : %d\n", t);
    t++;
  }
}

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
void _main();
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  _main();
}
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
????????????????????????????????? ??§?????§
??????????????? ??§?????§ ???????´<_??? ?????? Welcome to My Coding Space!
???????????? ??? ?´_???`??????/??? ???i
?????????????????????????????? ???  |???|
????????? /?????? /??£??£??£??£/??????|
??? ???_(__??????/???    ???/ .| .|????????????
??? ????????????/????????????/??????u??????
---------------------------------------------------------------------------------------------------*/

#define INF "#"
int N, A, S, G, X[401], Y[401];
string LAB[401];
string ans[401];
//---------------------------------------------------------------------------------------------------
void sol() {
  rep(i, 0, A) cin >> X[i] >> Y[i] >> LAB[i];

  vector<string> v;
  rep(i, 0, A) v.push_back(LAB[i]);

  rep(i, 0, N) ans[i] = INF;
  ans[G] = "";

  rep(i, 0, N * 6) rep(j, 0, A) {
    int a = X[j], b = Y[j];
    if (ans[b] == INF)
      continue;
    if (ans[a] == INF)
      ans[a] = LAB[j] + ans[b];
    else {
      string aa = ans[a], bb = LAB[j] + ans[b];
      if (bb < aa) {
        ans[a] = bb;
      }
    }
  }

  string pre = ans[S];

  rep(i, 0, N * 6) rep(j, 0, A) {
    int a = X[j], b = Y[j];
    if (ans[b] == INF)
      continue;
    if (ans[a] == INF)
      ans[a] = LAB[j] + ans[b];
    else {
      string aa = ans[a], bb = LAB[j] + ans[b];
      if (bb < aa) {
        ans[a] = bb;
      }
    }
  }

  if (ans[S] < pre || ans[S] == INF) {
    printf("NO\n");
    return;
  }

  printf("%s\n", ans[S].c_str());
}
//---------------------------------------------------------------------------------------------------
void _main() {
  int t = 1;
  while (cin >> N >> A >> S >> G) {
    if (N == 0)
      return;
    sol();
    // fprintf(stderr, "Finish : %d\n", t); t++;
  }
}