吃奶酪--dfs

版权声明:转载请附上地址 https://blog.csdn.net/weixin_44574520/article/details/89602498

luogu 1433

在这里插入图片描述

Code:

#include <bits/stdc++.h>
using namespace std;

int n,vis[20];
double x[20],y[20],ans=0;

inline void init_() {
	freopen("a.txt","r",stdin);
}

inline void clean_() {
	memset(vis,0,sizeof(vis));
}

inline void readda_() {
	clean_();
	scanf("%d",&n);
	for(int i=1;i<=n;i++) {
		scanf("%lf%lf",&x[i],&y[i]);
	}
}

inline double len_(double a,double b,double aa,double bb) {
	return sqrt( (a-aa)*(a-aa) + (b-bb)*(b-bb) );
}

inline void dfs_(double a,double b,int cnt,double dis) {
	if(cnt==n) {
		ans=min(ans,dis);
		return;
	}
	if(dis>=ans) return;
	for(int i=1;i<=n;i++) {
		if(vis[i]) continue;
		vis[i]=1;
		double pp=len_(a,b,x[i],y[i]);
		dfs_(x[i],y[i],cnt+1,dis+pp);
		vis[i]=0;
	}
	return;
}

inline void work_() {
	ans=999999999;
	dfs_(0,0,0,0);
	printf("%.2lf",ans);
}

int main() {
	init_();
	readda_();
	work_();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44574520/article/details/89602498