我的搜索怎么了?
查看原帖
我的搜索怎么了?
1374261
gaohongyuan楼主2025/7/2 15:22
#include<bits/stdc++.h>
using namespace std;

struct node
{
	double x,y;
}a[100];
struct node2
{
    double x;
    int id;
}c[20];

int n,cnt,cnt2;
bool b[100];
double ans=1e9,t[100][100];

bool cmp(node2 a,node2 b)
{
    return a.x>b.x;
}

void dfs(int k,int last,double sum)
{
    cnt++;
	if(k==n)
	{
		ans=min(ans,sum);
		return;
	}
    if(cnt>5e7)return;
    cnt2=0;
	for(int i=1;i<=n;i++)
	if(!b[i])
	{
        cnt2++;
        c[cnt2].x=t[last][i];
        c[cnt2].id=i;
	}
	sort(c+1,c+1+cnt2,cmp);
    for(int i=1;i<=ceil(cnt2/2.0)+1;i++)
    {
        if (sum+t[last][i]>ans) return;
        b[c[i].id]=1;
        dfs(k+1,c[i].id,sum+c[i].x);
        b[c[i].id]=0;
    }
}

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	cin>>a[i].x>>a[i].y;
	for(int i=0;i<=n;i++)
	for(int j=0;j<=n;j++)
	t[i][j]=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
	dfs(0,0,0);
	printf("%.2lf",ans);
    return 0;
}

打死不用 dp,搜索wa

2025/7/2 15:22
加载中...