Language: C++

/*
 * @Author: SugarSBN
 * @Date: 2018-07-14 19:06:53
 * @Language:   C++
 */
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<sstream>
#include<fstream>
using namespace std;
const int MAXN=2e8+11;
const double pi=acos(-1);
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef double dbl;
#define For(a,b,c) for (int (a)=(b);(a)<(c);(a)++)
#define foreach(iter,V) for (__typeof((V.begin()) iter=(V).begin();iter!=(V).end();iter++)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
vector<pii> col[MAXN];
int col_kinds[MAXN],num_of_colkinds=0;
int fa[MAXN];
int getfa(int u){
    return fa[u] == u ? u : fa[u] = getfa(fa[u]);
}
vi new_g[MAXN];
int right_point;
int n, m;
int a, b, c;
int main(){
  //  freopen("in.txt", "r", stdin);
    scanf("%d%d", &n, &m);
    right_point = n;
    bool vis[MAXN]={0};
    For(i,0,m){
        scanf("%d%d%d", &a, &b, &c);
        a--;
        b--;
        c--;
        if (!vis[c]){
            col_kinds[num_of_colkinds++] = c;
            vis[c] = 1;
        }
        col[c].pb(mp(a, b));
    }
    For(i, 0, n) fa[i] = i;
    For(i,0,num_of_colkinds){
        For(j,0,col[col_kinds[i]].size()){
            int fx = getfa(col[col_kinds[i]][j].fi);
            int fy = getfa(col[col_kinds[i]][j].se);
            fa[fx] = fy;
        }
        int ptr[MAXN]={0};
        For(j,0,col[col_kinds[i]].size()){
            if (!ptr[getfa(col[col_kinds[i]][j].fi)]){
                new_g[col[col_kinds[i]][j].fi].pb(right_point);
                new_g[right_point].pb(col[col_kinds[i]][j].fi);
                ptr[getfa(col[col_kinds[i]][j].fi)] = right_point;
                right_point++;
            }else{
                new_g[col[col_kinds[i]][j].fi].pb(ptr[getfa(col[col_kinds[i]][j].fi)]);
                new_g[ptr[getfa(col[col_kinds[i]][j].fi)]].pb(col[col_kinds[i]][j].fi);
            }
             if (!ptr[getfa(col[col_kinds[i]][j].se)]){
                new_g[col[col_kinds[i]][j].se].pb(right_point);
                new_g[right_point].pb(col[col_kinds[i]][j].se);
                ptr[getfa(col[col_kinds[i]][j].se)] = right_point;
                right_point++;
            }else{
                new_g[col[col_kinds[i]][j].se].pb(ptr[getfa(col[col_kinds[i]][j].se)]);
                new_g[ptr[getfa(col[col_kinds[i]][j].se)]].pb(col[col_kinds[i]][j].se);
            }
        }
    }
    int dis[MAXN];
    For(i, 0, right_point) dis[i] = -2;
    dis[0] = 0;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty()){
        int v=Q.front();
        Q.pop();
        For(i,0,new_g[v].size()){
            if(dis[new_g[v][i]]==-2){
                Q.push(new_g[v][i]);
                dis[new_g[v][i]] = dis[v] + 1;
            }
        }
    }
    printf("%d\n", dis[n - 1] / 2);
    return 0;
}


Compiler output:
/tmp/cc1NOf5t.o: in function `getfa(int)':
a.cc:(.text+0x1b): relocation truncated to fit: R_X86_64_PC32 against symbol `fa' defined in .bss section in /tmp/cc1NOf5t.o
a.cc:(.text+0x37): relocation truncated to fit: R_X86_64_PC32 against symbol `fa' defined in .bss section in /tmp/cc1NOf5t.o
a.cc:(.text+0x56): relocation truncated to fit: R_X86_64_PC32 against symbol `fa' defined in .bss section in /tmp/cc1NOf5t.o
a.cc:(.text+0x6d): relocation truncated to fit: R_X86_64_PC32 against symbol `fa' defined in .bss section in /tmp/cc1NOf5t.o
/tmp/cc1NOf5t.o: in function `main':
a.cc:(.text+0x8a): relocation truncated to fit: R_X86_64_PC32 against symbol `m' defined in .bss section in /tmp/cc1NOf5t.o
a.cc:(.text+0x94): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/cc1NOf5t.o
a.cc:(.text+0xb1): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/cc1NOf5t.o
a.cc:(.text+0xb7): relocation truncated to fit: R_X86_64_PC32 against symbol `right_point' defined in .bss section in /tmp/cc1NOf5t.o
a.cc:(.text+0xe3): relocation truncated to fit: R_X86_64_PC32 against symbol `c' defined in .bss section in /tmp/cc1NOf5t.o
a.cc:(.text+0xed): relocation truncated to fit: R_X86_64_PC32 against symbol `b' defined in .bss section in /tmp/cc1NOf5t.o
a.cc:(.text+0xf7): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status