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


#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef int ll;
struct node {
  short x = 0, y = 0;
  int p = 0;
} a[20];
long long ans[20];
short mi[20], m1[20];
short px[20][20], py[20][20];
int main(void) {

  short n;
  scanf("%hd", &n);
  for (short i = 0; i < n; i++)
    scanf("%hd%hd%d", &a[i].x, &a[i].y, &a[i].p);
  int ma = 1;
  for (short i = 0; i <= n; i++)
    ans[i] = 1000000000000000;
  for (short i = 0; i < n; i++)
    ma *= 3;
  for (short i = 0; i < n; i++)
    m1[i] = min(abs(a[i].x), abs(a[i].y));
  for (short i = 0; i < n; i++) {
    for (short j = 0; j < n; j++) {
      px[i][j] = (short)abs(a[i].x - a[j].x);
      py[i][j] = (short)abs(a[i].y - a[j].y);
    }
  }
  for (int zhu = 0; zhu < ma; zhu++) {
    int ppk = zhu;
    short cnt = 0;
    int st = ma / 3;
    for (short i = 0; i < n; i++)
      mi[i] = m1[i];
    for (short i = 0; i < n; i++, st /= 3) {
      int now = ppk / st;
      if (now == 2) {
        cnt++;
        for (ll j = 0; j < n; j++)
          if (mi[j] > px[i][j])
            mi[j] = px[i][j];
      } else if (now == 1) {
        cnt++;
        for (ll j = 0; j < n; j++)
          if (mi[j] > py[i][j])
            mi[j] = py[i][j];
      }
      ppk -= st * now;
    }
    long long sum = 0;
    for (short i = 0; i < n; i++)
      sum += (long long)mi[i] * a[i].p;
    ans[cnt] = min(ans[cnt], sum);
  }
  for (ll i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef int ll;
struct node {
  short x = 0, y = 0;
  int p = 0;
} a[20];
long long ans[20];
short mi[20], m1[20];
short px[20][20], py[20][20];
int main(void) {

  short n;
  scanf("%hd", &n);
  for (short i = 0; i < n; i++)
    scanf("%hd%hd%d", &a[i].x, &a[i].y, &a[i].p);
  int ma = 1;
  for (short i = 0; i <= n; i++)
    ans[i] = 1000000000000000;
  for (short i = 0; i < n; i++)
    ma *= 3;
  for (short i = 0; i < n; i++)
    m1[i] = min(abs(a[i].x), abs(a[i].y));
  for (short i = 0; i < n; i++) {
    for (short j = 0; j < n; j++) {
      px[i][j] = (short)abs(a[i].x - a[j].x);
      py[i][j] = (short)abs(a[i].y - a[j].y);
    }
  }
  for (int zhu = 0; zhu < ma; zhu++) {
    int ppk = zhu;
    short cnt = 0;
    int st = ma / 3;
    for (short i = 0; i < n; i++)
      mi[i] = m1[i];
    for (short i = 0; i < n; i++, st /= 3) {
      int now = ppk / st;
      if (now == 2) {
        cnt++;
        for (ll j = 0; j < n; j++)
          if (mi[j] > px[i][j])
            mi[j] = px[i][j];
      } else if (now == 1) {
        cnt++;
        for (ll j = 0; j < n; j++)
          if (mi[j] > py[i][j])
            mi[j] = py[i][j];
      }
      ppk -= st * now;
    }
    long long sum = 0;
    for (short i = 0; i < n; i++)
      sum += (long long)mi[i] * a[i].p;
    ans[cnt] = min(ans[cnt], sum);
  }
  for (ll i = 0; i <= n; i++)
    printf("%lld\\n", ans[i]);
}