Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long int64;
typedef pair< int64, int > Pi;
const int64 INF = 1LL << 60;
struct edge {
  int to, cost;
};
vector< edge > graph[400004];
int64 min_cost[400004];
int H, W, K;
vector< Pi > X[100001], Y[100001];
 
int64 Dijkstra()
{
  fill_n(min_cost, 400004, INF);
  priority_queue< Pi, vector< Pi >, greater< Pi > > Que;
  min_cost[K - 2] = 0;
  Que.push(Pi(0, K - 2));
  while(!Que.empty()) {
    Pi p = Que.top(); Que.pop();
    if(min_cost[p.second] < p.first) continue;
    if(p.second == K - 1) return(p.first);
    for(int i = 0; i < graph[p.second].size(); i++) {
      const e = graph[p.second][i];
      int64 next = p.first + e.cost;
      if(min_cost[e.to] > next) {
        min_cost[e.to] = next;
        Que.push(Pi(next, e.to));
      }
    }
  }
  return(-1);
}
 
 
int main()
{
  cin >> W >> H >> K;
  for(int i = 0; i < K; i++) {
    int x, y;
    cin >> x >> y;
    X[x].push_back(Pi(y, i));
    Y[y].push_back(Pi(x, i));
  }
 
  X[1].push_back(Pi(1, K));
  Y[1].push_back(Pi(1, K));
 
  X[W].push_back(Pi(H, K + 1));
  Y[H].push_back(Pi(W, K + 1));
 
  K += 2;
 
  for(int i = 0; i < 100001; i++) {
    sort(X[i].begin(), X[i].end());
    sort(Y[i].begin(), Y[i].end());
    for(int j = 1; j < X[i].size(); ++j) {
      const Pi &prev = X[i][j - 1], &curr = X[i][j];
      graph[prev.second].push_back((edge){curr.second, abs(prev.first - curr.first)});
      graph[curr.second].push_back((edge){prev.second, abs(prev.first - curr.first)});
    }
    for(int j = 1; j < Y[i].size(); ++j) {
      const Pi &prev = Y[i][j - 1], &curr = Y[i][j];
      graph[prev.second + K].push_back((edge){curr.second + K, abs(prev.first - curr.first)});
      graph[curr.second + K].push_back((edge){prev.second + K, abs(prev.first - curr.first)});
    }
  }
 
 
  for(int i = 0; i < K - 2; i++) {
    graph[i].push_back((edge){i + K, 1});
    graph[i + K].push_back((edge){i, 1});
  }
  graph[K - 1 + K].push_back((edge){K - 1, 0});  cout << Dijkstra() << endl;
}

Compiler output:
a.cc: In function 'int64 Dijkstra()':
a.cc:25:13: error: 'e' does not name a type
   25 |       const e = graph[p.second][i];
      |             ^
a.cc:26:30: error: 'e' was not declared in this scope
   26 |       int64 next = p.first + e.cost;
      |                              ^
a.cc: In function 'int main()':
a.cc:60:59: warning: narrowing conversion of 'std::abs((((long long int)prev.std::pair<long long int, int>::first) - ((long long int)curr.std::pair<long long int, int>::first)))' from 'long long int' to 'int' [-Wnarrowing]
   60 |       graph[prev.second].push_back((edge){curr.second, abs(prev.first - curr.first)});
      |                                                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:61:59: warning: narrowing conversion of 'std::abs((((long long int)prev.std::pair<long long int, int>::first) - ((long long int)curr.std::pair<long long int, int>::first)))' from 'long long int' to 'int' [-Wnarrowing]
   61 |       graph[curr.second].push_back((edge){prev.second, abs(prev.first - curr.first)});
      |                                                        ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:65:67: warning: narrowing conversion of 'std::abs((((long long int)prev.std::pair<long long int, int>::first) - ((long long int)curr.std::pair<long long int, int>::first)))' from 'long long int' to 'int' [-Wnarrowing]
   65 |       graph[prev.second + K].push_back((edge){curr.second + K, abs(prev.first - curr.first)});
      |                                                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:66:67: warning: narrowing conversion of 'std::abs((((long long int)prev.std::pair<long long int, int>::first) - ((long long int)curr.std::pair<long long int, int>::first)))' from 'long long int' to 'int' [-Wnarrowing]
   66 |       graph[curr.second + K].push_back((edge){prev.second + K, abs(prev.first - curr.first)});
      |                                                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~