Language: C++

#include <bits/stdc++.h>

using namespace std;

using ll = long long int;

#define REP(i, N) for (int i = 0; i < (N); ++i)
#define scanInt(x) int x; scanf("%d", &(x)); getchar();
#define scanLLInt(x) ll x; scanf("%lld", &(x)); getchar();
#define scanVecInt(A, N) vector<int> A(N); REP(i, N){scanf("%d", &(A[i])); getchar();}
#define scanVecLLInt(A, N) vector<ll> A(N); REP(i, N){scanf("%lld", &(A[i])); getchar();}

int main()
{
    scanInt(N); // 鬆らせ縺ｮ謨ｰ
    scanInt(M); // 霎ｺ縺ｮ謨ｰ
    vector<unordered_map<int, int>> edges1(N); // 蜷・らせ縺九ｉ蜃ｺ繧玖ｾｺ・磯らせ縺ｧ讀懃ｴ｢・・    vector<unordered_map<int, unordered_set<int>>> edges2(N); // 蜷・らせ縺九ｉ蜃ｺ繧玖ｾｺ・井ｼ夂､ｾ縺ｧ讀懃ｴ｢・・    unordered_set<int> company;
    unordered_map<int, unordered_set<int>> stations; // 蜷・ｼ夂､ｾ縺ｮ霍ｯ邱壹↓螻槭☆繧矩ｧ・分蜿ｷ
    REP(i, M){ // 霎ｺ縺ｮ謨ｰ縺ｯM
        scanInt(pi); --pi;
        scanInt(qi); --qi;
        scanInt(ci); // 霎ｺ縺ｮ髟ｷ縺輔〒縺ｯ縺ｪ縺丈ｼ夂､ｾ逡ｪ蜿ｷ
        edges1[pi].emplace(qi, ci);
        edges1[qi].emplace(pi, ci);
        edges2[pi][ci].emplace(qi);
        edges2[qi][ci].emplace(pi);
        company.emplace(ci);
        stations[ci].emplace(pi);
        stations[ci].emplace(qi);
    }
    vector<unordered_set<int>> lines; // 繧ｳ繧ｹ繝・縺ｧ陦後￠繧矩ｧ・・髮・粋
    vector<unordered_set<int>> edges3(N); // 蜷・らせ縺九ｉ鬧・寔蜷医ｒ讀懃ｴ｢
    for (auto& c : company){
        while (!stations[c].empty()){
            unordered_set<int> reached;
            unordered_set<int> next;
            next.emplace(*stations[c].begin()); // 縺ｨ繧翫≠縺医★1縺､隕∫ｴ繧貞・繧後ｋ
            while (!next.empty()){
                const int p = *next.begin();
                for (auto& q : edges2[p][c]){
                    if (reached.count(q)) continue;
                    next.emplace(q);
                }
                next.erase(p);
                reached.emplace(p);
            }
            lines.emplace_back();
            for (auto& r : reached){
                edges3[r].emplace(lines.size() - 1);
                lines.back().emplace(r);
                stations[c].erase(r);
            }
        }
    }
    auto dijkstra = [&](int start, int goal){
        if (start == goal) return 0;
        auto comp = [](pair<int, ll>& l, pair<int, ll>& r){return l.second > r.second;};
        priority_queue<pair<int, ll>, vector<pair<int, ll>>, decltype(comp)> pq(comp);
        vector<char> finished(N, false);
        pq.emplace(start, 0);
        finished[start] = true;

        while (!pq.empty()){
            const int next_node = get<0>(pq.top());
            const int temp_cost = get<1>(pq.top());
            pq.pop();
            if (next_node == goal) return temp_cost;
            finished[next_node] = true;
            // for (auto& node : edges[next_node]){
            for (auto& c : edges3[next_node]){
                for (auto& r : lines[c]){
                    if (finished[r]) continue;
                    pq.emplace(r, temp_cost + 1);
                }
            }
        }
        return -1;
    };
    cout << dijkstra(0, N - 1) << endl;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:25:9: error: 'edges2' was not declared in this scope; did you mean 'edges1'?
   25 |         edges2[pi][ci].emplace(qi);
      |         ^~~~~~
      |         edges1
a.cc:27:9: error: 'company' was not declared in this scope
   27 |         company.emplace(ci);
      |         ^~~~~~~
a.cc:33:20: error: 'company' was not declared in this scope
   33 |     for (auto& c : company){
      |                    ^~~~~~~
a.cc:40:32: error: 'edges2' was not declared in this scope; did you mean 'edges3'?
   40 |                 for (auto& q : edges2[p][c]){
      |                                ^~~~~~
      |                                edges3