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

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
// #define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
typedef int Def;
int n, m, w;
vp in;
class DIJ {
public:
  struct edge {
    Def to, cost;
  };
  vector<vector<edge>> G;
  vector<vector<Def>> d; // distance
  void dij() {
    d = vector<vector<Def>>(w, vector<Def>(n + m + 1, inf));
    d[w - 3][0] = d[w - 2][0] = 0; // now oktime ->time
    priority_queue<tp> q;
    q.push(tp(0, w - 2, 0)); // time now v_0 n-1を通ったか
    q.push(tp(0, w - 3, 0)); // time now v_0 n-1を通ったか
    while (!q.empty()) {
      Def cost, pos, t;
      tie(cost, pos, t) = q.top();
      cost *= -1;
      q.pop();
      if (cost > d[pos][t])
        continue;
      rep(i, w) if (i != pos) {
        int j = abs(in[pos].first - in[i].first) +
                abs(in[pos].second - in[i].second) - 1 - t;
        j = max(0, j);
        //				if(i==w-1&&j)continue;
        Def to = i;
        Def ncost = cost + j; // e.cost*v;
        Def nt = j;
        if (abs(in[pos].first - in[to].first) +
                abs(in[pos].second - in[to].second) - 1 >
            t + nt)
          continue;
        if (ncost < d[to][nt]) {
          d[to][nt] = ncost;
          q.push(tp(-ncost, to, nt));
        }
      }
    }
    Def out = inf;
    rep(i, n + m + 1) out = min(out, d[w - 1][i]);
    cout << out << endl;
    //		cout<<d[w-1][0]<<endl;
  }
};
int main() {
  cin >> n >> m >> w;
  in = vp(w + 3);
  rep(i, w) cin >> in[i].first >> in[i].second;
  in[w] = {1, 0};
  in[w + 1] = {0, 1};
  in[w + 2] = {n, m + 1};
  w += 3;
  DIJ dij;
  dij.dij();
}


Error: TLE

Fixed submission:

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
// #define int ll
typedef int Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-7;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
typedef int Def;
int n, m, w;
vp in;
class DIJ {
public:
  struct edge {
    Def to, cost;
  };
  vector<vector<edge>> G;
  vector<vector<Def>> d; // distance
  void dij() {
    d = vector<vector<Def>>(w, vector<Def>(n + m + 1, inf));
    d[w - 3][0] = d[w - 2][0] = 0; // now oktime ->time
    priority_queue<tp> q;
    q.push(tp(0, w - 2, 0)); // time now v_0 n-1を通ったか
    q.push(tp(0, w - 3, 0)); // time now v_0 n-1を通ったか
    while (!q.empty()) {
      Def cost, pos, t;
      tie(cost, pos, t) = q.top();
      cost *= -1;
      q.pop();
      if (cost > d[pos][t])
        continue;
      rep(i, w) if (i != pos) {
        int W = abs(in[pos].first - in[i].first) +
                abs(in[pos].second - in[i].second) - 1 - t;
        W = max(0, W);
        loop(j, W, n + m + 1) {
          if (i == w - 1 && j)
            continue;
          Def to = i;
          Def ncost = cost + j; // e.cost*v;
          Def nt = j;
          if (abs(in[pos].first - in[to].first) +
                  abs(in[pos].second - in[to].second) - 1 >
              t + nt)
            continue;
          if (ncost < d[to][nt]) {
            d[to][nt] = ncost;
            q.push(tp(-ncost, to, nt));
          }
        }
      }
    }
    Def out = inf;
    cout << d[w - 1][0] << endl;
  }
};
int main() {
  cin >> n >> m >> w;
  in = vp(w + 3);
  rep(i, w) cin >> in[i].first >> in[i].second;
  in[w] = {1, 0};
  in[w + 1] = {0, 1};
  in[w + 2] = {n, m + 1};
  w += 3;
  DIJ dij;
  dij.dij();
}