Language: C++

#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;

struct edge {
	int to, cost;
	edge(int t, int c):to(t), cost(c) {}
};

vector<vector<edge> > es;
typedef pair<int, int> P;

int dijkstra(int s, int g) {
	priority_queue<P, vector<P>, greater<P> > que;
	vector<int> d(g + 1, INT_MAX);
	que.push(P(0, s));
	d[s] = 0;
	
	while(!que.empty()) {
		P p = que.top();
		que.pop();
		int v = p.second;
		if(d[v] < p.first)
			continue;

		if(v == g)
			return p.first;

		for(int i = 0; i < es[v].size(); ++i) {
			edge e = es[v][i];
			if(d[e.to] > d[v] + e.cost) {
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int main() {
	cin.tie(false);
	ios::sync_with_stdio(false);

	vector<pair<string, int> > gt;
	map<string, int> label;

	int n;
	cin >> n;
	cin.ignore();

	es.resize(n + 1);

	for(int i = 0; i < n; ++i) {
		string s;
		getline(cin, s);

		if(s[s.size() - 1] == ':') {
			es[i].push_back(edge(i + 1, 0));
			label.insert(make_pair(s.substr(0, s.size() - 1), i));
		}
		else {
			es[i].push_back(edge(i + 1, 1));
			s = s.substr(5, s.size() - 6);
			gt.push_back(make_pair(s, i));
		}
	}

	for(int i = 0; i < gt.size(); ++i)
		es[gt[i].second].push_back(edge(label[gt[i].first], 0));

	cout << dijkstra(0, n) << endl;

	return EXIT_SUCCESS;
}

Compiler output:
a.cc: In function 'int dijkstra(int, int)':
a.cc:20:30: error: 'INT_MAX' was not declared in this scope
   20 |         vector<int> d(g + 1, INT_MAX);
      |                              ^~~~~~~
a.cc:7:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    6 | #include <utility>
  +++ |+#include <climits>
    7 | #include <vector>
a.cc: In function 'int main()':
a.cc:45:16: error: no matching function for call to 'std::basic_istream<char>::tie(bool)'
   45 |         cin.tie(false);
      |         ~~~~~~~^~~~~~~
In file included from /usr/include/c++/14/ios:46,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/bits/basic_ios.h:299:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>* std::basic_ios<_CharT, _Traits>::tie() const [with _CharT = char; _Traits = std::char_traits<char>]'
  299 |       tie() const
      |       ^~~
/usr/include/c++/14/bits/basic_ios.h:299:7: note:   candidate expects 0 arguments, 1 provided
/usr/include/c++/14/bits/basic_ios.h:311:7: note: candidate: 'std::basic_ostream<_CharT, _Traits>* std::basic_ios<_CharT, _Traits>::tie(std::basic_ostream<_CharT, _Traits>*) [with _CharT = char; _Traits = std::char_traits<char>]'
  311 |       tie(basic_ostream<_CharT, _Traits>* __tiestr)
      |       ^~~
/usr/include/c++/14/bits/basic_ios.h:311:43: note:   no known conversion for argument 1 from 'bool' to 'std::basic_ostream<char>*'
  311 |       tie(basic_ostream<_CharT, _Traits>* __tiestr)
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
a.cc: In function 'int dijkstra(int, int)':
a.cc:42:1: warning: control reaches end of non-void function [-Wreturn-type]
   42 | }
      | ^