Problem p00517 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int, int> pii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = 1; i < n; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define MOD (ll)(1e9 + 7)
#define ADD(a, b) a = ((a) + (b)) % MOD
#define FIX(a) ((a) % MOD + MOD) % MOD

#define INF 1e9

int n, w, h;

int main() {
  cin >> w >> h >> n;
  int res = 0;
  int bx, by;
  cin >> bx >> by;
  --bx;
  --by;
  int x, y;
  REPR(i, n) {
    cin >> x >> y;
    --x;
    --y;
    int tx = x - bx, ty = y - by;
    if (tx * ty >= 0) {
      res += max(abs(tx), abs(ty));
    } else {
      res += abs(tx) + abs(ty);
    }
    bx = x;
    by = y;
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef complex<double> P;
typedef pair<int, int> pii;
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define REPR(i, n) for (ll i = 1; i < n; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG_VEC(v)                                                           \\
  cout << #v << ":";                                                           \\
  REP(i, v.size()) cout << " " << v[i];                                        \\
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define MOD (ll)(1e9 + 7)
#define ADD(a, b) a = ((a) + (b)) % MOD
#define FIX(a) ((a) % MOD + MOD) % MOD

#define INF 1e9

struct edge {
  int from, to, w;
};
bool operator<(const edge &e, const edge &f) {
  return e.w != f.w         ? e.w > f.w
         : e.from != f.from ? e.from < f.from
                            : e.to < f.to;
}

int n, w, h;
// http://www.prefield.com/algorithm/graph/dijkstra.html
vi dijkstra(vector<vector<edge>> g, int s) {
  vi dist(w * h);
  int n = g.size();
  dist.assign(n, INF);
  dist[s] = 0;
  priority_queue<edge> Q; // "e < f" <=> "e.weight > f.weight"
  for (Q.push((edge){-2, s, 0}); !Q.empty();) {
    edge e = Q.top();
    Q.pop();
    // if (prev[e.to] != -1) continue;
    REP(i, g[e.to].size()) {
      edge f = g[e.to][i];
      if (dist[f.to] > e.w + f.w) {
        dist[f.to] = e.w + f.w;
        Q.push((edge){f.from, f.to, e.w + f.w});
      }
    }
  }
  return dist;
}
int getpos(int x, int y) { return x * h + y; }

void addE(vector<vector<edge>> &g, int from, int to) {
  g[from].push_back((edge){from, to, 1});
  g[to].push_back((edge){to, from, 1});
}

int main() {
  cin >> w >> h >> n;
  vector<vector<edge>> g(w * h);
  REP(i, w) REP(j, h) {
    if (i < w - 1) {
      addE(g, getpos(i, j), getpos(i + 1, j));
    }
    if (j < h - 1) {
      addE(g, getpos(i, j), getpos(i, j + 1));
    }
    if (i < w - 1 && j < h - 1) {
      addE(g, getpos(i, j), getpos(i + 1, j + 1));
    }
  }
  int res = 0;
  int x, y;
  cin >> x >> y;
  --x;
  --y;
  int bef = getpos(x, y);
  REPR(i, n) {
    cin >> x >> y;
    --x;
    --y;
    int to = getpos(x, y);
    res += dijkstra(g, bef)[to];
    bef = to;
  }
  cout << res << endl;
  return 0;
}