Language: C++

#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
#include <set>
#include <string>

typedef int node_t;

struct arrow_t {
    node_t source;
    node_t sink;
    std::string label;
};

struct entry_t {
    std::string label;
    std::set<node_t> nodes;

    entry_t() {}
    entry_t(const std::string& label) : label(label) {}
};

typedef std::list<entry_t> queue_t;

int compare(const std::string& s1, const std::string& s2) {
    size_t size1 = s1.size();
    size_t size2 = s2.size();
    for (int i = 0; ; i++) {
        if (i == size1) {
            return i == size2 ? 0 : -1;
        } else if (i == size2) {
            return 1;
        }
        int c = ((int) s1[i]) - ((int) s2[i]);
        if (c != 0) {
            return c < 0 ? -2 : 2;
        }
    }
    return 0;
}

queue_t::iterator insert(queue_t& queue, const std::string& label) {
    for (queue_t::iterator it = queue.begin(); it != queue.end(); it++) {
        switch (compare(it->label, label)) {
        case -2:
            return queue.end();
        case -1:
            continue;
        case 0:
            return it;
        case 1:
            return queue.insert(it, entry_t(label));
        case 2:
            queue.erase(it, queue.end());
            break;
        }
    }
    return queue.insert(queue.end(), entry_t(label));
}

std::set<node_t> collect(int n, const std::vector<arrow_t>& arrows, node_t goal) {
    std::vector<std::vector<node_t> > ins(n);
    for (const arrow_t& arrow : arrows) {
        ins[arrow.sink].push_back(arrow.source);
    }

    std::set<node_t> result;
    std::vector<node_t> stack;
    stack.push_back(goal);
    while (!stack.empty()) {
        node_t node = stack.back();
        stack.pop_back();

        result.insert(node);

        for (int source : ins[node]) {
            if (result.find(source) == result.end()) {
                stack.push_back(source);
            }
        }
    }
    return result;
}

std::string calc(int n, const std::vector<arrow_t>& arrows, node_t start, node_t goal) {
    const std::set<node_t>& reachable_set = collect(n, arrows, goal);
    if (reachable_set.find(start) == reachable_set.end()) {
        return "NO";
    }

    size_t max = 0;
    std::vector<std::vector<const arrow_t*> > outs(n);
    for (const arrow_t& arrow : arrows) {
        if (reachable_set.find(arrow.sink) != reachable_set.end()) {
            max = std::max(max, arrow.label.size());
            outs[arrow.source].push_back(&arrow);
        }
    }

    int limit = n * max;

    std::string result;
    queue_t queue;
    queue.push_back(entry_t());
    queue.front().nodes.insert(start);
    while (!queue.empty()) {
        std::string label = queue.front().label;
        std::set<node_t> nodes = queue.front().nodes;
        queue.pop_front();

        result += label;
        if (nodes.find(goal) != nodes.end()) {
            return result;
        }
        if (result.size() > limit) {
            return "NO";
        }

        int size = label.size();
        for (entry_t& entry : queue) {
            entry.label.erase(0, size);
        }

        for (node_t node : nodes) {
            for (const arrow_t* arrow : outs[node]) {
                queue_t::iterator it = insert(queue, arrow->label);
                if (it != queue.end()) {
                    it->nodes.insert(arrow->sink);
                }
            }
        }
    }
    return "?";
}

int main() {
    while (true) {
        int n, a, s, g;
        std::cin >> n >> a >> s >> g;
        if (n == 0) {
            break;
        }
        std::array<arrow_t> arrows(a);
        for (int i = 0; i < a; i++) {
            arrow_t& arrow = arrows[i];
            std::cin >> arrow.source >> arrow.sink >> arrow.label;
        }
break;
        std::cout << calc(n, arrows, s, g) << std::endl;
    }
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:144:27: error: wrong number of template arguments (1, should be 2)
  144 |         std::array<arrow_t> arrows(a);
      |                           ^
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_pair.h:99:12: note: provided for 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:146:36: error: invalid types 'int[int]' for array subscript
  146 |             arrow_t& arrow = arrows[i];
      |                                    ^
a.cc:150:30: error: invalid initialization of reference of type 'const std::vector<arrow_t>&' from expression of type 'int'
  150 |         std::cout << calc(n, arrows, s, g) << std::endl;
      |                              ^~~~~~
a.cc:86:53: note: in passing argument 2 of 'std::string calc(int, const std::vector<arrow_t>&, node_t, node_t)'
   86 | std::string calc(int n, const std::vector<arrow_t>& arrows, node_t start, node_t goal) {
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~