洛谷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 }

猜你喜欢

转载自www.cnblogs.com/572354941hnit/p/10806104.html