救命!
  • 板块灌水区
  • 楼主Lian_zy
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/1/14 16:45
  • 上次更新2024/1/14 20:05:58
查看原帖
救命!
923248
Lian_zy楼主2024/1/14 16:45

P1433 吃奶酪

WA+TLE+AC 10分求助

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 1e5 + 10;
struct node {
	int id;
	double x, y;
} a[maxn];
int n, p[maxn];
double ans = 1e9, d[maxn][20];
int lowbit(int x) {
	return x & -x;
}
double dist(node a, node b) {
	return sqrt((a.x - b.x) * (a.x - b.x) - (a.y - b.y) * (a.y - b.y));
}
void dfs(int step, int v, double sum, node pre) {
	if (step == n + 1) {
		ans = min(ans, sum);
		return ;
	}
	if (sum >= ans || d[v][pre.id] < sum && d[v][pre.id] > 0) return ;
	d[v][pre.id] = sum;
	int t = v, lt;
	while (t) {
		lt = lowbit(t);
		t -= lt;
		dfs(step + 1, v - lt, sum + dist(pre, a[p[lt]]), a[p[lt]]);
	}
	return ;
}
int main() {
	scanf("%d", &n);
	for (int i = 0; i < 16; i++) p[1 << i] = i + 1;
	for (int i = 1; i <= n; i++) {
		scanf("%lf %lf", &a[i].x, &a[i].y);
		a[i].id = i;
	}
	dfs(1, (1 << n) - 1, 0, node{0, 0});
	printf("%.2lf", ans);
	return 0;
}
2024/1/14 16:45
加载中...