P1433 吃奶酪 dfs+剪枝 被卡常90pts
作者:
多米尼克領主的致意
,
2024-05-03 19:40:22
,
所有人可见
,
阅读 4
正解用状态压缩dp
dfs代码:
#include<bits/stdc++.h>
int n;
double mx = 0x3f3f3f3f;
int clk;
int st[20];
using namespace std;
struct zb
{
double x, y;
}zb[20];
void dfs(double x, double y, double sum, int cnt)
{
if(sum >= mx)
{
clk++;
return;//最优性剪枝
}
if(cnt >= n)//可行性剪枝
{
mx = min(mx, sum);
// cout << mx << endl;
clk = 0;
return;
}
for(int i = 1;i <= n;i++)
{
if(clk >= 35000000)return;
if(st[i])continue;
double xx = zb[i].x, yy = zb[i].y;
double distance = sqrt((xx - x) * (xx - x) + (yy - y) * (yy - y));
// if(distance >= mx)return;
st[i] = true;
dfs(xx, yy, sum + distance, cnt + 1);
st[i] = false;
}
}
int main()
{
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
//cout << sqrt(0);
//return 0;
cin >> n;
for(int i = 1;i <= n;i++)cin >> zb[i].x >> zb[i].y;
dfs(0, 0, (double)0, 0);
printf("%.2f", mx);
return 0;
}