Language: C++

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
#define rep(i,n)for(int i=0;i<(int)(n);i++)
using namespace std;
typedef long long ll;

ll d[200000][2];
int x[200000],y[200000];
vector<int>X[100000],Y[100000];
struct st{
	int p;bool b;ll c;
};
bool operator<(st&a,st&b)->const{
	return a.c>b.c;
}
int main() {
	int m,n,k;scanf("%d%d%d",&m,&n,&k);
	rep(i,k){
		scanf("%d%d",&x[i],&y[i]);x[i]--;y[i]--;
		X[x[i]].push_back(i);Y[y[i]].push_back(i);
	}
	priority_queue<st>que;
	memset(d,0x3f,sizeof(d));
	for(int&u:X[0]){
		d[u][1]=y[u];
		que.push({u,1,d[u][1]});
	}
	while(!que.empty()){
		st p=que.top();que.pop();
		if(d[p.p][p.b]!=p.c)continue;
		if(p.b){
			for(int&u:X[x[p.p]]){
				if(d[u][1]>p.c+abs(y[p.p]-y[u])){
					d[u][1]=p.c+abs(y[p.p]-y[u]);
					que.push({u,1,d[u][1]});
				}
			}
			for(int&u:Y[y[p.p]]){
				if(d[u][0]>p.c+abs(x[p.p]-x[u])+1){
					d[u][0]=p.c+abs(x[p.p]-x[u])+1;
					que.push({u,0,d[u][0]});
				}
			}
		}
		else{
			for(int&u:X[x[p.p]]){
				if(d[u][1]>p.c+abs(y[p.p]-y[u])+1){
					d[u][1]=p.c+abs(y[p.p]-y[u])+1;
					que.push({u,1,d[u][1]});
				}
			}
			for(int&u:Y[y[p.p]]){
				if(d[u][0]>p.c+abs(x[p.p]-x[u])){
					d[u][0]=p.c+abs(x[p.p]-x[u]);
					que.push({u,0,d[u][0]});
				}
			}
		}
	}
	ll Min=INF;
	for(int&u:X[m-1]){
		Min=min(Min,d[u][1]+abs(y[u]-n+1));
		Min=min(Min,d[u][0]+1+abs(y[u]-n+1));
	}
	for(int&u:Y[n-1]){
		Min=min(Min,d[u][0]+abs(x[u]-m+1));
		Min=min(Min,d[u][1]+1+abs(x[u]-m+1));
	}
	if(Min==INF)puts("-1");
	else printf("%lld\n",Min);
}

Compiler output:
a.cc:13:28: error: ISO C++ forbids declaration of 'type name' with no type [-fpermissive]
   13 | bool operator<(st&a,st&b)->const{
      |                            ^~~~~
a.cc:13:1: error: 'operator<' function with trailing return type not declared with 'auto' type specifier
   13 | bool operator<(st&a,st&b)->const{
      | ^~~~
In file included from /usr/include/c++/14/string:49,
                 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/stl_function.h: In instantiation of 'constexpr bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = st]':
/usr/include/c++/14/bits/predefined_ops.h:196:23:   required from 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = __gnu_cxx::__normal_iterator<st*, std::vector<st, std::allocator<st> > >; _Value = st; _Compare = std::less<st>]'
  196 |         { return bool(_M_comp(*__it, __val)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:140:48:   required from 'void std::__push_heap(_RandomAccessIterator, _Distance, _Distance, _Tp, _Compare&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<st*, vector<st, allocator<st> > >; _Distance = long int; _Tp = st; _Compare = __gnu_cxx::__ops::_Iter_comp_val<less<st> >]'
  140 |       while (__holeIndex > __topIndex && __comp(__first + __parent, __value))
      |                                          ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_heap.h:216:23:   required from 'void std::push_heap(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<st*, vector<st, allocator<st> > >; _Compare = less<st>]'
  216 |       std::__push_heap(__first, _DistanceType((__last - __first) - 1),
      |       ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  217 |                        _DistanceType(0), _GLIBCXX_MOVE(__value), __cmp);
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_queue.h:747:16:   required from 'void std::priority_queue<_Tp, _Sequence, _Compare>::push(value_type&&) [with _Tp = st; _Sequence = std::vector<st, std::allocator<st> >; _Compare = std::less<st>; value_type = st]'
  747 |         std::push_heap(c.begin(), c.end(), comp);
      |         ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:26:11:   required from here
   26 |                 que.push({u,1,d[u][1]});
      |                 ~~~~~~~~^~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_function.h:405:20: error: no match for 'operator<' (operand types are 'const st' and 'const st')
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51:
/usr/include/c++/14/bits/stl_pair.h:1045:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator<(const pair<_T1, _T2>&, const pair<_T1, _T2>&)'
 1045 |     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1045:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const st' is not derived from 'const std::pair<_T1, _T2>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:67:
/usr/include/c++/14/bits/stl_iterator.h:448:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_Iterator>&)'
  448 |     operator<(const reverse_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:448:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const st' is not derived from 'const std::reverse_iterator<_Iterator>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:493:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const reverse_iterator<_Iterator>&, const reverse_iterator<_IteratorR>&)'
  493 |     operator<(const reverse_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:493:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const st' is not derived from 'const std::reverse_iterator<_Iterator>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:1694:5: note: candidate: 'template<class _IteratorL, class _IteratorR> constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_IteratorR>&)'
 1694 |     operator<(const move_iterator<_IteratorL>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1694:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const st' is not derived from 'const std::move_iterator<_IteratorL>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~
/usr/include/c++/14/bits/stl_iterator.h:1760:5: note: candidate: 'template<class _Iterator> constexpr bool std::operator<(const move_iterator<_IteratorL>&, const move_iterator<_IteratorL>&)'
 1760 |     operator<(const move_iterator<_Iterator>& __x,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1760:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/stl_function.h:405:20: note:   'const st' is not derived from 'const std::move_iterator<_IteratorL>'
  405 |       { return __x < __y; }
      |                ~~~~^~~~~