Language: C++

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

#define int long long

typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>void chmax(T &t,U f){if(t<f)t=f;}

struct data{
    int v,c,d;
    data(int v,int c,int d):v(v),c(c),d(d){}
    bool operator<(const data &d)const{
        return c>d.c;
    }
};

const int SIZE=200000;
const int INF=1001001001001001001LL;
int W,H,K;
int X[SIZE],Y[SIZE];

vpint row[SIZE],col[SIZE];

int to[SIZE][4];
int cost[SIZE][4];

int dist[SIZE][2];


signed main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);

    cin>>W>>H>>K;
    rep(i,K){
        cin>>X[i]>>Y[i];
        X[i]--;Y[i]--;
        row[Y[i]].pb(pint(X[i],i));
        col[X[i]].pb(pint(Y[i],i));
    }

    rep(i,H)sort(all(row[i]));
    rep(i,W)sort(all(col[i]));

    memset(to,-1,sizeof(to));

    rep(i,H){
        rep(j,(int)row[i].size()-1){
            pint &v=row[i][j],&u=row[i][j+1];
            to[v.se][1]=u.se;cost[v.se][1]=u.fi-v.fi;
            to[u.se][3]=v.se;cost[u.se][3]=u.fi-v.fi;
        }
    }
    rep(i,W){
        rep(j,(int)col[i].size()-1){
            pint &v=col[i][j],&u=col[i][j+1];
            to[v.se][2]=u.se;cost[v.se][2]=u.fi-v.fi;
            to[u.se][0]=v.se;cost[u.se][0]=u.fi-v.fi;
        }
    }

    if(col[0].size()==0){
        cout<<-1<<endl;
        return 0;
    }

    pint start=col[0][0];
    fill_n(*dist,SIZE*2,INF);
    dist[start.se][0]=start.fi;
    priority_queue<data>que;
    que.push(data(start.se,start.fi,0));

    while(que.size()){
        data d=que.top();que.pop();
        if(dist[d.v][d.d]<d.c)continue;
        if(dist[d.v][1-d.d]>d.c+1){
            dist[d.v][1-d.d]=d.c+1;
            que.push(data(d.v,d.c+1,1-d.d));
        }

        rep(i,4){
            if(i%2!=d.d)continue;
            int t=to[d.v][i],c=cost[d.v][i];
            if(t==-1)continue;
            if(dist[t][d.d]<=d.c+c)continue;
            dist[t][d.d]=d.c+c;
            que.push(data(t,d.c+c,d.d));
        }
    }
8 9 15
3 1
3 2
3 7
3 8
1 1
4 5
4 3
5 6
5 8
6 3
6 2
7 5
8 9
8 6
8 5
    int mi=INF;

    rep(i,K){
        if(X[i]==W-1){
            chmin(mi,dist[i][0]+H-1-Y[i]);
        }
        if(Y[i]==H-1){
            chmin(mi,dist[i][1]+W-1-X[i]);
        }
    }

    cout<<mi<<endl;
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:81:24: error: template argument 1 is invalid
   81 |     priority_queue<data>que;
      |                        ^
a.cc:81:24: error: template argument 2 is invalid
a.cc:81:24: error: template argument 3 is invalid
a.cc:82:9: error: request for member 'push' in 'que', which is of non-class type 'int'
   82 |     que.push(data(start.se,start.fi,0));
      |         ^~~~
a.cc:82:14: error: reference to 'data' is ambiguous
   82 |     que.push(data(start.se,start.fi,0));
      |              ^~~~
In file included from /usr/include/c++/14/string:53,
                 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/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:20:8: note:                 'struct data'
   20 | struct data{
      |        ^~~~
a.cc:84:15: error: request for member 'size' in 'que', which is of non-class type 'int'
   84 |     while(que.size()){
      |               ^~~~
a.cc:85:9: error: reference to 'data' is ambiguous
   85 |         data d=que.top();que.pop();
      |         ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:20:8: note:                 'struct data'
   20 | struct data{
      |        ^~~~
a.cc:85:30: error: request for member 'pop' in 'que', which is of non-class type 'int'
   85 |         data d=que.top();que.pop();
      |                              ^~~
a.cc:86:17: error: 'd' was not declared in this scope
   86 |         if(dist[d.v][d.d]<d.c)continue;
      |                 ^
a.cc:87:17: error: 'd' was not declared in this scope
   87 |         if(dist[d.v][1-d.d]>d.c+1){
      |                 ^
a.cc:89:17: error: request for member 'push' in 'que', which is of non-class type 'int'
   89 |             que.push(data(d.v,d.c+1,1-d.d));
      |                 ^~~~
a.cc:89:22: error: reference to 'data' is ambiguous
   89 |             que.push(data(d.v,d.c+1,1-d.d));
      |                      ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:20:8: note:                 'struct data'
   20 | struct data{
      |        ^~~~
a.cc:93:21: error: 'd' was not declared in this scope
   93 |             if(i%2!=d.d)continue;
      |                     ^
a.cc:94:22: error: 'd' was not declared in this scope
   94 |             int t=to[d.v][i],c=cost[d.v][i];
      |                      ^
a.cc:96:34: error: 'c' was not declared in this scope
   96 |             if(dist[t][d.d]<=d.c+c)continue;
      |                                  ^
a.cc:97:30: error: 'c' was not declared in this scope
   97 |             dist[t][d.d]=d.c+c;
      |                              ^
a.cc:98:17: error: request for member 'push' in 'que', which is of non-class type 'int'
   98 |             que.push(data(t,d.c+c,d.d));
      |                 ^~~~
a.cc:98:22: error: reference to 'data' is ambiguous
   98 |             que.push(data(t,d.c+c,d.d));
      |                      ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:20:8: note:                 'struct data'
   20 | struct data{
      |        ^~~~
a.cc:101:2: error: expected ';' before numeric constant
  101 | 8 9 15
      |  ^~
      |  ;
a.cc:121:19: error: 'mi' was not declared in this scope; did you mean 'i'?
  121 |             chmin(mi,dist[i][0]+H-1-Y[i]);
      |                   ^~
      |                   i
a.cc:124:19: error: 'mi' was not declared in this scope; did you mean 'i'?
  124 |             chmin(mi,dist[i][1]+W-1-X[i]);
      |                   ^~
      |                   i
a.cc:128:11: error: 'mi' was not declared in this scope; did you mean 'mp'?
  128 |     cout<<mi<<endl;
      |           ^~
      |           mp