Language: C++

#include<cstdio>
#include<vector>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
const int INF = 100000000;
typedef pair<int, string> P;

vector<int> to[40];
vector<string> name[40];

int n, a, s, g;

bool used[40];
vector<P> que[2]; // queueとみせかけてvector
string ans;
string temp;
string nil = "";

int dp[40][40];

void wf() {
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}

bool canGoal(int i) {
  return dp[i][g] < INF;
}

void push(int j, int x, string plus) {
  for (int i = 0; i < to[x].size(); i++) {
    string newn = plus + name[x][i];
    if (temp.empty() || temp > newn) {
      que[j].clear();
      temp = newn;
    } else if (temp < newn) {
      if (newn.substr(0, temp.size()) == temp) {
        que[j].push_back(P(to[x][i], newn.substr(temp.size(), newn.size())));
      }
      return;
    }
    que[j].push_back(P(to[x][i], nil));
  }
}

bool check() {
  wf();
  push(0, s, nil);
  ans += temp;
  temp.clear();
  for (int now = 0, next = 1;;) {
    //for (int i = 0; i < n; i++) printf("%d ",used[i]);
    //printf("\n");
    if (que[now].size() == 0) return false;
    for (int i = 0; i < que[now].size(); i++) {
      int x = que[now][i].first;
      if (used[x]) {
        if (canGoal(x)) return false;
        else {
          used[x] = true;
          continue;
        }
      }
      used[x] = true;
      push(next, x, que[now][i].second);
      if (x == g) {
        return true;
      }
    }
    que[now].clear();
    ans += temp;
    temp.clear();
    if (now == 0) now = 1, next = 0;
    else now = 0, next = 1;
  }
  return false;
}



int main() {
  while (1) {
    scanf("%d%d%d%d", &n, &a, &s, &g);
    if (n == 0) break;
    for (int i = 0; i < n; i++) to[i].clear(), name[i].clear(), used[i] = false;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) dp[i][j] = INF;
    que[0].clear();
    que[1].clear();
    ans.clear();
    int x, y;
    string lab;
    for (int i = 0; i < a; i++) {
      scanf("%d%d", &x, &y);
      cin >> lab;
      dp[x][y] = 1;
      to[x].push_back(y);
      name[x].push_back(lab);
    }
    //for (int i = 0;#include<cstdio>
#include<vector>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
const int INF = 100000000;
typedef pair<int, string> P;

vector<int> to[40];
vector<string> name[40];

int n, a, s, g;

bool used[40];
vector<P> que[2]; // queueとみせかけてvector
string ans;
string realans;
string temp;
string nil = "";

void push(int j, int x, string plus) {
  for (int i = 0; i < to[x].size(); i++) {
    string newn = plus + name[x][i];
    if (temp.empty() || temp > newn) {
      que[j].clear();
      temp = newn;
    } else if (temp < newn) {
      if (newn.substr(0, temp.size()) == temp) {
        que[j].push_back(P(to[x][i], newn.substr(temp.size(), newn.size())));
      }
      return;
    }
    que[j].push_back(P(to[x][i], nil));
  }
}

bool check() {
  push(0, s, nil);
  ans += temp;
  temp.clear();
  for (int now = 0, next = 1, counter = 0; counter < n; counter++) {
    //for (int i = 0; i < n; i++) printf("%d ",used[i]);
    //printf("\n");
    if (que[now].size() == 0) return true;
    for (int i = 0; i < que[now].size(); i++) {
      int x = que[now][i].first;
      //if (used[x]) {
        ///!!!
        //if (canGoal(x)) return true;
        //else {
        //  used[x] = true;
        //  continue;
        //}
      //}
      used[x] = true;
      push(next, x, que[now][i].second);
      if (x == g) {
        string te = ans + que[now][i].second;
        if (realans.empty() || realans > te) realans = te;
        // return true;
      }
    }
    que[now].clear();
    ans += temp;
    temp.clear();
    if (now == 0) now = 1, next = 0;
    else now = 0, next = 1;
  }
  return true;
}



int main() {
  while (1) {
    scanf("%d%d%d%d", &n, &a, &s, &g);
    if (n == 0) break;
    for (int i = 0; i < n; i++) to[i].clear(), name[i].clear(), used[i] = false;
    que[0].clear();
    que[1].clear();
    ans.clear();
    int x, y;
    string lab;
    for (int i = 0; i < a; i++) {
      scanf("%d%d", &x, &y);
      cin >> lab;
      to[x].push_back(y);
      name[x].push_back(lab);
    }
    //for (int i = 0; i < n; i++) cout<<name[i]<<"\n";
    check();
    if (used[g]) cout << realans << "\n";
    else printf("NO\n");
    realans.clear();
  }
}
 i < n; i++) cout<<name[i]<<"\n";
    if (check()) cout << ans << "\n";
    else printf("NO\n");
  }
}



Compiler output:
a.cc: In function 'int main()':
a.cc:124:38: error: a function-definition is not allowed here before '{' token
  124 | void push(int j, int x, string plus) {
      |                                      ^
a.cc:140:11: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
  140 | bool check() {
      |           ^~
a.cc:140:11: note: remove parentheses to default-initialize a variable
  140 | bool check() {
      |           ^~
      |           --
a.cc:140:11: note: or replace parentheses with braces to value-initialize a variable
a.cc:140:14: error: a function-definition is not allowed here before '{' token
  140 | bool check() {
      |              ^
a.cc:177:9: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
  177 | int main() {
      |         ^~
a.cc:177:9: note: remove parentheses to default-initialize a variable
  177 | int main() {
      |         ^~
      |         --
a.cc:177:9: note: or replace parentheses with braces to value-initialize a variable
a.cc:177:12: error: a function-definition is not allowed here before '{' token
  177 | int main() {
      |            ^
a.cc:200:2: error: 'i' was not declared in this scope
  200 |  i < n; i++) cout<<name[i]<<"\n";
      |  ^