码迷,mamicode.com
首页 > 其他好文 > 详细

luogu P1433 吃奶酪

时间:2017-09-21 22:22:58      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:超过   names   amp   include   存储   size   oid   mat   cst   

原题链接:https://www.luogu.org/problem/show?pid=1433

 

虽然是一道思维难度不大的DFS,但是这其中比较重要的是可行性剪枝(这是本蒟蒻做的第一道剪枝)

本题的优化点有不少:预处理两点之间的距离,用邻接矩阵存储,搜索时直接调用即可。

当目前走到的距离已经超过现有的最小值,那么就无需继续搜索下去,直接返回即可。

(那些register 之类的,都是因为写了个死循环TLE了以为常数问题加的。。。)

 

#include<cstdio>
#include<cmath> 
#include<algorithm>
using namespace std;
double ans=99999999;
double e[20][20],x[20],y[20];
int b[20],n;
void dfs(double sum,int s,int l)
{
//    printf("%.2lf %d %d\n",sum,s,l);
    if(sum>=ans) return;
    if(s==n&&sum<ans) ans=sum;
    else
    {
        for(register int i=1;i<=n;i++)
        {
            if(b[i]==0)
            {
                b[i]=1;
                dfs(sum+e[l][i],s+1,i);
                b[i]=0;
            }
        }
    }
    return;
}
int main()
{
    scanf("%d",&n);
    for(register int i=1;i<=n;i++) scanf("%lf %lf",&x[i],&y[i]);
    for(register int i=0;i<=n;i++)
    {
        for(register int j=0;j<=n;j++)
        {
            if(i==j) continue;
            e[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
        }
    }
    dfs(0,0,0);
    printf("%.2lf",ans);
    return 0;
}

 

luogu P1433 吃奶酪

标签:超过   names   amp   include   存储   size   oid   mat   cst   

原文地址:http://www.cnblogs.com/zeroform/p/7571548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!