【解题报告】洛谷P1433 吃奶酪

【解题报告】洛谷P1433 吃奶酪

题目链接

https://www.luogu.com.cn/problem/P1433

思路

这道题目出现在搜索的题单里,我就当搜索做了吧

然后就直接做了

发现超时一个点

怎么办

玄学优化,当递归次数超过某个值的时候直接返回当前最优解

似乎是参数的选择是凭借运气吧

大概似乎貌似好像

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <ctime>
using namespace std;
const int maxn=20;
int n,cnt;
double ans=999999.0;
double x[maxn],y[maxn],dis[maxn][maxn];
bool vis[maxn]={false};
double dist;
double get(int i,int j)
{
	if(dis[i][j]!=0) return dis[i][j]; 
	return dis[i][j]=dis[j][i]=(double)(sqrt(1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j])));
}
void dfs(int now,int dep)
{
	cnt++;
	if(cnt>=25000000)
	{
    	printf("%.2lf",ans);
    	exit(0);
	}
	if(dist+0.00001>ans) return ;
	if(dep>n)
	{
		//cout<<"wow! I can update ans to:"<<dist<<'\n';
		ans=min(ans,dist);
		return ;
	}
	for(int i=1;i<=n;i++)
	{
		if(vis[i]) continue;
		//cout<<"I'm visiting:"<<now<<" and to fuck "<<i<<"\n";
		vis[i]=true;
		double add=get(now,i);
		dist+=add;
		if(dep==n-1)
		dfs(i,dep+2);
		else
		dfs(i,dep+1);
		dist-=add;
		vis[i]=false;
	}
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>x[i]>>y[i];
	dfs(0,0);
	printf("%.2lf\n",ans);
	return 0;
}
/*
15
0 0
1 1
1 -1
-1 1
-1 -1
2 2
2 0
2 -2
0 -2
-2 -2
-2 0
-2 2
0 2
1 3
1 4
*/ 
posted @ 2021-10-15 19:53  wweiyi  阅读(43)  评论(0编辑  收藏  举报
js脚本