Language: C++

#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;

struct Node{
	int to, dis;
	Node(int _to, int _dis){to = _to, dis = _dis;}
	bool operator < (const Node &A) const{
		return dis > A.dis;
	}
};

vector <Node> g[100010];
vector <Node> from[200010];
int n, m, tot;
int dis[200010][2];
priority_queue <Node> q;

void Dij(int s, int p){
	for (int i = 1; i < tot; i++){
		dis[i][p] = INF;
	}
	dis[s][p] = 1;
	q.push(Node(s, 1));
	while (!q.empty()){
		Node t = q.top(); q.pop();
		int u = t.to;
		if (dis[u][p] < t.dis) continue;
		for (int i = 0; i < from[u].size(); i++){
			int v = from[u][i].to;
			if (dis[v][p] > dis[u][p] + from[u][i].dis){
				dis[v][p] = dis[u][p] + from[u][i].dis;
				q.push(Node(v, dis[v][p]));
			}
		}
	}
}

map <int, bool> hash[100010];

void make_new(int x, int c, int num){
	if (hash[x][c]) return ;
//	cout << "    " << x << " " << c << " " << num << endl;
	hash[x][c] = 1;
	from[x].push_back(Node(num, 0));
	for (int i = 0; i < g[x].size(); i++){
		if (g[x][i].dis != c) continue;
		int v = g[x][i].to;
		make_new(v, c, num);
	}
}

int change(int x, int c, int t){
	if (hash[x][c]) return 0;
	make_new(x, c, tot++);
	return 1;
}

int main(){
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++){
		int u, v, c;
		scanf("%d%d%d", &u, &v, &c);
		g[u].push_back(Node(v, c));
		g[v].push_back(Node(u, c));
	}
	tot = n + 1;
	for (int i = 1; i <= n; i++){
//		cout << i << endl;
		for (int j = 0; j < g[i].size(); j++){
//			cout << "  " << j << endl;
			change(i, g[i][j].dis, tot);
		}
	}
	for (int i = 1; i <= n; i++){
		for (int j = 0; j < from[i].size(); j++){
			int u = from[i][j].to;
			for (int k = j + 1; k < from[i].size(); k++){
				int v = from[i][k].to;
				from[u].push_back(Node(v, 1));
				from[v].push_back(Node(u, 1));
			}
		}
	}
	Dij(1, 0);
	Dij(n, 1);
	int ans = INF;
	for (int i = n + 1; i < tot; i++){
		ans = min(ans, dis[i][0] + dis[i][1]);
	}
	printf("%d\n", ans == INF ? -1 : ans - 1);
	return 0;
}


Compiler output:
a.cc: In function 'void make_new(int, int, int)':
a.cc:43:13: error: reference to 'hash' is ambiguous
   43 |         if (hash[x][c]) return ;
      |             ^~~~
In file included from /usr/include/c++/14/string_view:50,
                 from /usr/include/c++/14/bits/basic_string.h:47,
                 from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:40:17: note:                 'std::map<int, bool> hash [100010]'
   40 | map <int, bool> hash[100010];
      |                 ^~~~
a.cc:45:9: error: reference to 'hash' is ambiguous
   45 |         hash[x][c] = 1;
      |         ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:40:17: note:                 'std::map<int, bool> hash [100010]'
   40 | map <int, bool> hash[100010];
      |                 ^~~~
a.cc: In function 'int change(int, int, int)':
a.cc:55:13: error: reference to 'hash' is ambiguous
   55 |         if (hash[x][c]) return 0;
      |             ^~~~
/usr/include/c++/14/bits/functional_hash.h:59:12: note: candidates are: 'template<class _Tp> struct std::hash'
   59 |     struct hash;
      |            ^~~~
a.cc:40:17: note:                 'std::map<int, bool> hash [100010]'
   40 | map <int, bool> hash[100010];
      |                 ^~~~