Problem p00750 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

typedef long long ll;
typedef unsigned long long ull;
typedef string str;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
  int n, m, start, goal;
  while (cin >> n >> m >> start >> goal, n | m | start | goal) {
    vector<vector<string>> DP(n, vector<string>(500, "~"));
    vector<vector<pair<string, int>>> E(n);
    REP(i, m) {
      int x, y;
      string s;
      cin >> x >> y >> s;
      E[x].push_back({s, y});
    }
    bool clear = false;
    string ans;
    priority_queue<pair<string, int>, vector<pair<string, int>>,
                   greater<pair<string, int>>>
        Q;
    Q.push({"", start});
    DP[start][0] = "";
    while (!Q.empty()) {
      pair<string, int> q = Q.top();
      Q.pop();
      if (q.first.length() > 500) {
        continue;
      }
      if (q.second == goal) {
        if ((q.first.length() < 400))
          clear = true;
        ans = q.first;
        break;
      }
      REP(i, E[q.second].size()) {
        if (DP[E[q.second][i].second]
              [q.first.length() + E[q.second][i].first.length()] >
            q.first + E[q.second][i].first) {
          DP[E[q.second][i].second]
            [q.first.length() + E[q.second][i].first.length()] =
                q.first + E[q.second][i].first;
          Q.push({q.first + E[q.second][i].first, E[q.second][i].second});
        }
      }
    }

    if (!clear) {
      cout << "NO" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define RSORT(c) sort((c).rbegin(), (c).rend())
#define CLR(a) memset((a), 0, sizeof(a))
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

typedef long long ll;
typedef unsigned long long ull;
typedef string str;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vd> vvd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int main() {
  int n, m, start, goal;
  while (cin >> n >> m >> start >> goal, n | m | start | goal) {
    vector<vector<string>> DP(n, vector<string>(500, "~"));
    vector<vector<pair<string, int>>> E(n);
    REP(i, m) {
      int x, y;
      string s;
      cin >> x >> y >> s;
      E[x].push_back({s, y});
    }
    bool clear = false;
    string ans;
    priority_queue<pair<string, int>, vector<pair<string, int>>,
                   greater<pair<string, int>>>
        Q;
    Q.push({"", start});
    DP[start][0] = "";
    while (!Q.empty()) {
      pair<string, int> q = Q.top();
      Q.pop();
      if (q.first.length() > 500) {
        continue;
      }
      if (q.second == goal) {
        if ((q.first.length() < 400))
          clear = true;
        ans = q.first;
        break;
      }
      REP(i, E[q.second].size()) {
        if (q.first.length() + E[q.second][i].first.length() >= 500)
          continue;
        if (DP[E[q.second][i].second]
              [q.first.length() + E[q.second][i].first.length()] >
            q.first + E[q.second][i].first) {
          DP[E[q.second][i].second]
            [q.first.length() + E[q.second][i].first.length()] =
                q.first + E[q.second][i].first;
          Q.push({q.first + E[q.second][i].first, E[q.second][i].second});
        }
      }
    }

    if (!clear) {
      cout << "NO" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}