Language: C++

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
int pa[111111],id[111111],cnt,rank[111111];
bool used[111111];
int n,m,x,y,z;
vector<pair<int,int> > g[1111111];
vector<int> gra[1111111];
void Init(int i)
{
	pa[i]=i;
	used[i]=0;
	id[i]=-1;	
	rank[i]=1;
}
int root(int x)
{
	if (pa[x]!=x) pa[x]=root(pa[x]);
	return pa[x];
}
void Union(int x,int y)
{
	int rx=root(x),ry=root(y);
	if (rx==ry) return;
	if (rank[rx]>rank[ry]) 
	{
		pa[ry]=rx;
		rank[rx]+=rank[ry];
	}
	else 
	{
		pa[rx]=ry;
		rank[ry]+=rank[rx];
	}
}
queue<int> q;
int dist[1111111];
void write(int x)
{
    if(x<0)putchar('-'),x=-x;
    if(x>9)write(x/10);
    putchar(x%10+'0');
}
int main()
{
	while(scanf("%d%d",&n,&m)!=-1)
	{
		for (int i=1;i<=n;i++) g[i].clear();
		for (int i=1;i<=1000000;i++) gra[i].clear();
		for (int i=1;i<=m;i++)
		{
			scanf("%d%d%d",&x,&y,&z);
			g[z].push_back(make_pair(x,y)); 
		}
		cnt=n;
		for (int i=1;i<=1000000;i++)
		{
			for (int j=0;j<g[i].size();j++)
			{
				Init(g[i][j].first);
				Init(g[i][j].second);
			}
			for (int j=0;j<g[i].size();j++)
			{
				Union(g[i][j].first,g[i][j].second);
			}
			for (int j=0;j<g[i].size();j++)
			{
				x=g[i][j].first;y=g[i][j].second;
				if (!used[x])
				{
					int r=root(x);
					if (!~id[r]) id[r]=++cnt;
					used[x]=1;
					gra[x].push_back(id[r]);
					gra[id[r]].push_back(x); 
				}
				if (!used[y])
				{
					int r=root(y);
					if (!~id[r]) id[r]=++cnt;
					used[y]=1;
					gra[y].push_back(id[r]);
					gra[id[r]].push_back(y);  
				}
			}
		}
		memset(dist,-1,sizeof(dist));
		q.push(1);dist[1]=0;
		while(!q.empty())
		{
			x=q.front();q.pop();
			for (int i=0;i<gra[x].size();i++)
			{
				int to=gra[x][i];
				if (!~dist[to])
				{
					dist[to]=dist[x]+1;
					q.push(to); 
				}
			}
		}
		if (!~dist[n]) printf("-1\n");
		else printf("%d\n",dist[n]/2);
	}
	return 0;
}

Compiler output:
a.cc: In function 'void Init(int)':
a.cc:17:9: error: reference to 'rank' is ambiguous
   17 |         rank[i]=1;
      |         ^~~~
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:31: note:                 'int rank [111111]'
    7 | int pa[111111],id[111111],cnt,rank[111111];
      |                               ^~~~
a.cc: In function 'void Union(int, int)':
a.cc:28:13: error: reference to 'rank' is ambiguous
   28 |         if (rank[rx]>rank[ry])
      |             ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:31: note:                 'int rank [111111]'
    7 | int pa[111111],id[111111],cnt,rank[111111];
      |                               ^~~~
a.cc:28:22: error: reference to 'rank' is ambiguous
   28 |         if (rank[rx]>rank[ry])
      |                      ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:31: note:                 'int rank [111111]'
    7 | int pa[111111],id[111111],cnt,rank[111111];
      |                               ^~~~
a.cc:31:17: error: reference to 'rank' is ambiguous
   31 |                 rank[rx]+=rank[ry];
      |                 ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:31: note:                 'int rank [111111]'
    7 | int pa[111111],id[111111],cnt,rank[111111];
      |                               ^~~~
a.cc:31:27: error: reference to 'rank' is ambiguous
   31 |                 rank[rx]+=rank[ry];
      |                           ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:31: note:                 'int rank [111111]'
    7 | int pa[111111],id[111111],cnt,rank[111111];
      |                               ^~~~
a.cc:36:17: error: reference to 'rank' is ambiguous
   36 |                 rank[ry]+=rank[rx];
      |                 ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:31: note:                 'int rank [111111]'
    7 | int pa[111111],id[111111],cnt,rank[111111];
      |                               ^~~~
a.cc:36:27: error: reference to 'rank' is ambiguous
   36 |                 rank[ry]+=rank[rx];
      |                           ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:7:31: note:                 'int rank [111111]'
    7 | int pa[111111],id[111111],cnt,rank[111111];
      |                               ^~~~