0 P1433 吃奶酪

https://www.luogu.org/problemnew/show/P1433

思路:深搜,不过加剪枝

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
#define maxn 16
int n;
struct node
{
 double  x,  y;//表示点横纵坐标
 bool vis;//标记有没有被搜索过
 node():vis(0){};
}Node[maxn];

double  a[17][17];//a[i][j]表示第i个点到第j个点的距离
double  res = 1<<30;//初始最短路径长度值
void dfs(int num, double  nowlength, int now)//分别表示走了多少个点,当前走的长度,当前所在点
{
 if(num == n)
 {
 
  if(nowlength < res)//更新值
   res = nowlength;
  return ;
 }
 if(nowlength > res)//剪枝,没必要搜索下个了
  return ;
  for(int i = 1; i <= n; i++)
  {
   if(Node[i].vis == 0)//没有走过的点
   {
    nowlength += a[now][i];//路径增加
    Node[i].vis = 1;//标记放完
    num++;//已走的点个数增加
    dfs(num, nowlength, i);
	//回溯
    nowlength -= a[now][i];
    Node[i].vis = 0;
    num--;
   }
  }
}
int main()
{
 ios::sync_with_stdio (false);
 //cin>>n;
 scanf("%d", &n);
 for(int i = 1;  i <= n; i++)
 {
  //cin>>Node[i].x;
  //cin>>Node[i].y;
	 scanf("%lf", &Node[i].x);
	 scanf("%lf", &Node[i].y);
  

 }

 Node[0].x = 0;
 Node[0].y = 0;
 for(int i = 0; i <= n; i++)//预处理第i个点到第j个点的距离
 {
  for(int j = 0; j <= n; j++)
   a[i][j] = sqrt(((Node[i].x - Node[j].x) *( Node[i].x - Node[j].x) + (Node[i].y - Node[j].y)* (Node[i].y - Node[j].y))*1.0);
 }
 dfs(0, 0, 0);
 //cout<<res<<endl;
 printf("%.2lf\n", res);
 //system("pause");
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值