剪枝求助
  • 板块P1433 吃奶酪
  • 楼主MVP_Harry
  • 当前回复11
  • 已保存回复11
  • 发布时间2020/6/13 07:38
  • 上次更新2023/11/7 00:46:30
查看原帖
剪枝求助
306962
MVP_Harry楼主2020/6/13 07:38

蒟蒻不会dp,只会爆搜,结果t了三个点,已经剪过一点枝了,但是当n = 11时,还是要循环近10810^8

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, m, n) for(int i = m; i <= n; i++)
#define per(i, m, n) for(int i = m; i >= n; i--)
const int INF = 0x3f3f3f3f;

int n;
double best = 10000000;

struct node {
	double x, y;
}a[20];

double distance(double x1, double y1, double x2, double y2) {
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

bool cmp(int a, int b) {
	return a > b;
}

bool cmp2(node a, node b) {
	return distance(a.x, a.y, 0, 0) < distance(b.x, b.y, 0, 0);
}

int main() {
	ll cnt = 0;
	int num[20];
	ios::sync_with_stdio(false);
	cin >> n;
	rep(i, 0, n) num[i] = i;
	a[0].x = 0, a[0].y = 0;
	rep(i, 1, n) {
		cin >> a[i].x >> a[i].y;
	}
	sort(a + 1, a + n + 1, cmp2);
	do {
		// cnt++;
		double dis = 0;
		for (int i = 1; i <= n; i++) {
			int prev = num[i - 1], now = num[i];
			dis += (distance(a[now].x, a[now].y, a[prev].x, a[prev].y));
			if (dis > best) {
				sort(num + i + 1, num + n + 1, cmp);
				break;
			}
		}
		best = min(dis, best);
	} while (next_permutation(num + 1, num + n + 1));
	// cout << cnt << endl;
	printf("%.2f\n", best);
    return 0;
}
2020/6/13 07:38
加载中...