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

// ?????¬???????????¬??????

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N;
double x[1010], y[1010];
double dp[1010][1010];

double dist(int s, int t) {
  double dfx = x[s] - x[t];
  double dfy = y[s] - y[t];
  return sqrt(dfx * dfx + dfy * dfy);
}

void solve() {
  rep(i, 0, 1010) rep(j, 0, 1010) dp[i][j] = INF;
  dp[0][0] = 0;

  queue<pii> q;
  q.push(pii(0, 0));
  while (!q.empty()) {
    pii cur = q.front();
    q.pop();
    int s = max(cur.first, cur.second), t = min(cur.first, cur.second);
    if (s == N - 1)
      continue;
    int nx = s + 1;
    double d1 = dist(t, nx), d2 = dist(s, nx);
    // t -> nx
    if (dp[nx][s] > dp[s][t] + d1) {
      dp[nx][s] = dp[s][t] + d1;
      q.push(pii(nx, s));
    }
    // s -> nx;
    if (dp[nx][t] > dp[s][t] + d2) {
      dp[nx][t] = dp[s][t] + d2;
      q.push(pii(nx, t));
    }
  }
}

signed main() {
  cin >> N;
  rep(i, 0, N) cin >> x[i] >> y[i];
  solve();

  double ans = INF;
  rep(i, 0, N - 1) {
    // printf("dp[N-1][%lld] = %.12f\\n", i, dp[N-1][i]);
    chmin(ans, dp[N - 1][i] + dist(N - 1, i));
  }
  printf("%.12f\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

// ?????¬???????????¬??????

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int N;
double x[1010], y[1010];
double dp[1010][1010];

double dist(int s, int t) {
  double dfx = x[s] - x[t];
  double dfy = y[s] - y[t];
  return sqrt(dfx * dfx + dfy * dfy);
}

void solve(int s, int t) {
  if (s < t)
    swap(s, t);
  if (s == N - 1)
    return;
  int nx = s + 1;
  double d1 = dist(t, nx), d2 = dist(s, nx);
  // t -> nx
  if (dp[nx][s] > dp[s][t] + d1) {
    dp[nx][s] = dp[s][t] + d1;
    solve(nx, s);
  }
  // s -> nx;
  if (dp[nx][t] > dp[s][t] + d2) {
    dp[nx][t] = dp[s][t] + d2;
    solve(nx, t);
  }
}

signed main() {
  cin >> N;
  rep(i, 0, N) cin >> x[i] >> y[i];
  rep(i, 0, 1010) rep(j, 0, 1010) dp[i][j] = INF;
  dp[0][0] = 0;
  solve(0, 0);

  double ans = INF;
  rep(i, 0, N - 1) {
    // printf("dp[N-1][%lld] = %.12f\\n", i, dp[N-1][i]);
    chmin(ans, dp[N - 1][i] + dist(N - 1, i));
  }
  printf("%.12f\\n", ans);
  return 0;
}