洛谷1433-DFS剪枝

题目链接 https://www.luogu.org/recordnew/show/18739088

解题思路 通过最短距离剪枝,只要超过这个距离,就重新计算,刚开始以为坐标只是整数,有个测试点是:

2

0.01 0.01

0.09 0.09 

结果参数没化成double,老年人眼力不好。。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 int n;
 7 double dx[16],dy[16];
 8 int ct;
 9 double dis=0x3f3f3f3f;
10 bool vis[16];
11 double ans;
12 void dfs(double x,double y,double s)
13 {
14     if(s>dis)
15     return ;
16     if(ct==n)
17     {
18         /*cout<<"s2: "<<s<<endl;
19         cout<<"dis: "<<dis<<endl;*/
20         dis=min(dis,s);
21         return;
22     }
23     for(int i=0;i<n;i++)
24     {
25         if(!vis[i]){
26         vis[i]=1;
27         ct++;
28         /*cout<<"i: "<<i<<endl;
29         cout<<"py: "<<dx[i]-x<<endl;*/
30         ans=sqrt((dx[i]-x)*(dx[i]-x)+(dy[i]-y)*(dy[i]-y));
31         /*cout<<"ans: "<<ans<<endl;
32         cout<<"s1: "<<s<<endl;*/
33         dfs(dx[i],dy[i],s+ans);
34         vis[i]=0;
35         ct--;
36         }
37     }
38 
39 
40 }
41 int main()
42 {
43     cin>>n;
44     for(int i=0;i<n;i++)
45         cin>>dx[i]>>dy[i];
46             dfs(0,0,0);
47             printf("%.2f",dis);
48 }

 

转载于:https://www.cnblogs.com/572354941hnit/p/10806104.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值