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

#include <bits/stdc++.h>
using namespace std;
#define int long long

bool isprime(int n) {
  double rootn = sqrt(n);
  if (n < 2) {
    return false;
  } else if (n == 2) {
    return true;
  } else if (n % 2 == 0) {
    return false;
  } else {
    for (int i = 3; i <= rootn; i += 2) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
}

int old_S(vector<tuple<int, int, int>> P, vector<int> tate, vector<int> yoko) {
  int dis[P.size()];
  for (int i = 0; i < P.size(); i++) {
    int min_dis = abs(get<1>(P[i]) - 10000);
    for (int j = 0; j < tate.size(); j++) {
      if (min_dis > abs(get<1>(P[i]) - tate[j])) {
        min_dis = abs(get<1>(P[i]) - tate[j]);
      }
    }
    for (int j = 0; j < yoko.size(); j++) {
      if (min_dis > abs(get<2>(P[i]) - yoko[j])) {
        min_dis = abs(get<2>(P[i]) - yoko[j]);
      }
    }
    dis[i] = min_dis;
  }

  int ans = 0;
  for (int i = 0; i < P.size(); i++) {
    ans += dis[i] * get<0>(P[i]);
  }

  return ans;
}

int S(vector<tuple<int, int, int>> P, vector<vector<int>> tate_min_dis,
      vector<vector<int>> yoko_min_dis, int n_tate, int n_yoko) {
  int sum = 0;
  for (int i = 0; i < P.size(); i++) {
    int min_dis = (tate_min_dis[n_tate][i] > yoko_min_dis[n_yoko][i])
                      ? yoko_min_dis[n_yoko][i]
                      : tate_min_dis[n_tate][i];
    sum += min_dis * get<0>(P[i]);
  }
  return sum;
}

int ll_abs(int n) {
  if (n > 0)
    return n;
  else
    return -n;
}

signed main() {
  // clock_t start = clock();
  // clock_t end = clock();

  int N;
  cin >> N;
  vector<tuple<int, int, int>> P;
  for (int i = 0; i < N; i++) {
    int Xi, Yi, Pi;
    cin >> Xi >> Yi >> Pi;
    P.push_back(make_tuple(Pi, Xi + 10000, Yi + 10000));
  }
  // sort(P.begin(), P.end());

  int pow_two = 1, pow_three = 1;
  for (int i = 0; i < N; i++) {
    pow_two *= 2;
    pow_three *= 3;
  }

  vector<vector<int>> tate_min_dis(pow_two, vector<int>(N));
  vector<vector<int>> yoko_min_dis(pow_two, vector<int>(N));
  for (int i = 0; i < pow_two; i++) {
    for (int j = 0; j < N; j++) {
      tate_min_dis[i][j] = ll_abs(get<1>(P[j]) - 10000);
      yoko_min_dis[i][j] = ll_abs(get<2>(P[j]) - 10000);
    }
  }

  for (int i = 0; i < pow_two; i++) {
    for (int j = 0; j < N; j++) {
      int tempi = i;
      for (int l = 0; l < N; l++) {
        if (tempi % 2 == 1) {
          if (tate_min_dis[i][j] > ll_abs(get<1>(P[l]) - get<1>(P[j]))) {
            tate_min_dis[i][j] = ll_abs(get<1>(P[l]) - get<1>(P[j]));
          }
          if (yoko_min_dis[i][j] > ll_abs(get<2>(P[l]) - get<2>(P[j]))) {
            yoko_min_dis[i][j] = ll_abs(get<2>(P[l]) - get<2>(P[j]));
          }
        }
        tempi /= 2;
      }
    }
  }

  int min_S[N + 1];
  int first_S = S(P, tate_min_dis, yoko_min_dis, 0, 0);
  for (int i = 0; i <= N; i++) {
    min_S[i] = first_S;
  }

  for (int i = 0; i < pow_three; i++) {
    int temp_i = i;
    int cnt = 0;

    int tate_pow = 0, yoko_pow = 0;
    for (int l = N - 1; l >= 0; l--) {
      if (temp_i % 3 == 1) {
        cnt++;
        tate_pow = tate_pow * 2 + 1;
        yoko_pow = yoko_pow * 2;
      } else if (temp_i % 3 == 2) {
        cnt++;
        tate_pow = tate_pow * 2;
        yoko_pow = yoko_pow * 2 + 1;
      } else {
        tate_pow = tate_pow * 2;
        yoko_pow = yoko_pow * 2;
      }
      temp_i /= 3;
    }

#if 0
        int now_S = S(P, tate_min_dis, yoko_min_dis, tate_pow, yoko_pow);
#else
    int now_S = 0;
    for (int j = 0; j < N; j++) {
#if 1
      int min_dis = (tate_min_dis[tate_pow][j] > yoko_min_dis[yoko_pow][j])
                        ? yoko_min_dis[yoko_pow][j]
                        : tate_min_dis[tate_pow][j];
#else
      int min_dis = tate_min_dis[tate_pow][j];
      if (tate_min_dis[tate_pow][j] > yoko_min_dis[yoko_pow][j]) {
        min_dis = yoko_min_dis[yoko_pow][j];
      }
#endif
      now_S += min_dis *= get<0>(P[j]);
    }
#endif
    if (min_S[cnt] > now_S) {
      min_S[cnt] = now_S;
    }
  }

  for (int i = 0; i <= N; i++) {
    cout << min_S[i] << endl;
  }

  // end = clock();
  // cout << "time: " << (double)(end-start)/CLOCKS_PER_SEC << " sec." << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long

bool isprime(int n) {
  double rootn = sqrt(n);
  if (n < 2) {
    return false;
  } else if (n == 2) {
    return true;
  } else if (n % 2 == 0) {
    return false;
  } else {
    for (int i = 3; i <= rootn; i += 2) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
}

int old_S(vector<tuple<int, int, int>> P, vector<int> tate, vector<int> yoko) {
  int dis[P.size()];
  for (int i = 0; i < P.size(); i++) {
    int min_dis = abs(get<1>(P[i]) - 10000);
    for (int j = 0; j < tate.size(); j++) {
      if (min_dis > abs(get<1>(P[i]) - tate[j])) {
        min_dis = abs(get<1>(P[i]) - tate[j]);
      }
    }
    for (int j = 0; j < yoko.size(); j++) {
      if (min_dis > abs(get<2>(P[i]) - yoko[j])) {
        min_dis = abs(get<2>(P[i]) - yoko[j]);
      }
    }
    dis[i] = min_dis;
  }

  int ans = 0;
  for (int i = 0; i < P.size(); i++) {
    ans += dis[i] * get<0>(P[i]);
  }

  return ans;
}

int S(vector<tuple<int, int, int>> P, vector<vector<int>> tate_min_dis,
      vector<vector<int>> yoko_min_dis, int n_tate, int n_yoko) {
  int sum = 0;
  for (int i = 0; i < P.size(); i++) {
    int min_dis = (tate_min_dis[n_tate][i] > yoko_min_dis[n_yoko][i])
                      ? yoko_min_dis[n_yoko][i]
                      : tate_min_dis[n_tate][i];
    sum += min_dis * get<0>(P[i]);
  }
  return sum;
}

int ll_abs(int n) {
  if (n > 0)
    return n;
  else
    return -n;
}

signed main() {
  int N;
  cin >> N;
  vector<tuple<int, int, int>> P;
  for (int i = 0; i < N; i++) {
    int Xi, Yi, Pi;
    cin >> Xi >> Yi >> Pi;
    P.push_back(make_tuple(Pi, Xi + 10000, Yi + 10000));
  }
  // sort(P.begin(), P.end());

  int pow_two = 1, pow_three = 1;
  for (int i = 0; i < N; i++) {
    pow_two *= 2;
    pow_three *= 3;
  }

  vector<vector<int>> tate_min_dis(pow_two, vector<int>(N));
  vector<vector<int>> yoko_min_dis(pow_two, vector<int>(N));
  for (int i = 0; i < pow_two; i++) {
    for (int j = 0; j < N; j++) {
      tate_min_dis[i][j] = ll_abs(get<1>(P[j]) - 10000);
      yoko_min_dis[i][j] = ll_abs(get<2>(P[j]) - 10000);
    }
  }

  for (int i = 0; i < pow_two; i++) {
    for (int j = 0; j < N; j++) {
      int tempi = i;
      for (int l = 0; l < N; l++) {
        if (tempi % 2 == 1) {
          if (tate_min_dis[i][j] > ll_abs(get<1>(P[l]) - get<1>(P[j]))) {
            tate_min_dis[i][j] = ll_abs(get<1>(P[l]) - get<1>(P[j]));
          }
          if (yoko_min_dis[i][j] > ll_abs(get<2>(P[l]) - get<2>(P[j]))) {
            yoko_min_dis[i][j] = ll_abs(get<2>(P[l]) - get<2>(P[j]));
          }
        }
        tempi /= 2;
      }
    }
  }

  int min_S[N + 1];
  int first_S = S(P, tate_min_dis, yoko_min_dis, 0, 0);
  for (int i = 0; i <= N; i++) {
    min_S[i] = first_S;
  }

  for (int i = 0; i < pow_three; i++) {
    int temp_i = i;
    int cnt = 0;

    int tate_pow = 0, yoko_pow = 0;
    for (int l = N - 1; l >= 0; l--) {
      if (temp_i % 3 == 1) {
        cnt++;
        tate_pow = tate_pow * 2 + 1;
        yoko_pow = yoko_pow * 2;
      } else if (temp_i % 3 == 2) {
        cnt++;
        tate_pow = tate_pow * 2;
        yoko_pow = yoko_pow * 2 + 1;
      } else {
        tate_pow = tate_pow * 2;
        yoko_pow = yoko_pow * 2;
      }
      temp_i /= 3;
    }

    int now_S = S(P, tate_min_dis, yoko_min_dis, tate_pow, yoko_pow);
    if (min_S[cnt] > now_S) {
      min_S[cnt] = now_S;
    }
  }

  for (int i = 0; i <= N; i++) {
    cout << min_S[i] << endl;
  }

  return 0;
}