【Luogu1433】吃奶酪

problem

solution

codes

#include
#include
#include
#include
using namespace std;
typedef double LD;
const int maxn = 55;
int n, vis[maxn];
LD a[maxn], b[maxn], dis[maxn][maxn], now, ans=(int)1e9;
void dfs(int c, int p){
    //cout<
    if(now>ans)return ;//最优化剪枝
    if(c==n){ ans = min(ans,now); return ;}
    vis[p] = 1;
    for(int i = 1; i <= n; i++){
        if(!vis[i]){
            now += dis[p][i];
            dfs(c+1,i);
            now -= dis[p][i];
        }
    }
    vis[p] = 0;
}

int main(){
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i = 1; i <= n; i++)
        cin>>a[i]>>b[i];
    //预处理两点距离
    for(int i = 0; i <= n; i++)
        for(int j = 0; j <= n; j++)
            dis[i][j] = sqrt((a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]));
    dfs(0,0);
    //cout<
    printf("%.2lf\n",ans);
    return 0;
}

你可能感兴趣的:(NOIP)