zoukankan      html  css  js  c++  java
  • 【解题报告】洛谷P1433 吃奶酪

    【解题报告】洛谷P1433 吃奶酪

    题目链接

    https://www.luogu.com.cn/problem/P1433

    思路

    这道题目出现在搜索的题单里,我就当搜索做了吧

    然后就直接做了

    发现超时一个点

    怎么办

    玄学优化,当递归次数超过某个值的时候直接返回当前最优解

    似乎是参数的选择是凭借运气吧

    大概似乎貌似好像

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <ctime>
    using namespace std;
    const int maxn=20;
    int n,cnt;
    double ans=999999.0;
    double x[maxn],y[maxn],dis[maxn][maxn];
    bool vis[maxn]={false};
    double dist;
    double get(int i,int j)
    {
    	if(dis[i][j]!=0) return dis[i][j]; 
    	return dis[i][j]=dis[j][i]=(double)(sqrt(1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j])));
    }
    void dfs(int now,int dep)
    {
    	cnt++;
    	if(cnt>=25000000)
    	{
        	printf("%.2lf",ans);
        	exit(0);
    	}
    	if(dist+0.00001>ans) return ;
    	if(dep>n)
    	{
    		//cout<<"wow! I can update ans to:"<<dist<<'
    ';
    		ans=min(ans,dist);
    		return ;
    	}
    	for(int i=1;i<=n;i++)
    	{
    		if(vis[i]) continue;
    		//cout<<"I'm visiting:"<<now<<" and to fuck "<<i<<"
    ";
    		vis[i]=true;
    		double add=get(now,i);
    		dist+=add;
    		if(dep==n-1)
    		dfs(i,dep+2);
    		else
    		dfs(i,dep+1);
    		dist-=add;
    		vis[i]=false;
    	}
    }
    int main()
    {
    	cin>>n;
    	for(int i=1;i<=n;i++)
    		cin>>x[i]>>y[i];
    	dfs(0,0);
    	printf("%.2lf
    ",ans);
    	return 0;
    }
    /*
    15
    0 0
    1 1
    1 -1
    -1 1
    -1 -1
    2 2
    2 0
    2 -2
    0 -2
    -2 -2
    -2 0
    -2 2
    0 2
    1 3
    1 4
    */ 
    
    本博文为wweiyi原创,若想转载请联系作者,qq:2844938982
  • 相关阅读:
    Metropolis-Hastings algorithm
    Base64编码原理
    修改远程端口号
    修改数据库配置文件
    Windows 2008下系统网站运行环境的搭建
    oracle 11 g数据库卸载(方法二)
    oracle11g的安装
    oracle 11g的卸载
    软件实施的技巧
    使用命令行快速打开系统文件
  • 原文地址:https://www.cnblogs.com/wweiyi2004/p/15412463.html
Copyright © 2011-2022 走看看