cogs 线型网络(状压dp)

/*
需要好大的空间.....
而且lowbit理解的不是很好
先放到博客里 以后慢慢研究
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define inf 999999999;
#define maxn 1050000
using namespace std;
int n,a[maxn];
double x[],y[],g[][],f[maxn][],ans=inf;
double Dis(double x1,double y1,double x2,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int l(int x)
{
return x&(-x);
}
int main()
{
freopen("linec.in","r",stdin);
freopen("linec.out","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lf%lf",&x[i],&y[i]);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
g[i][j]=Dis(x[i],y[i],x[j],y[j]);
for(int i=;i<=n;i++)
a[<<i]=i+;
for(int i=;i<(<<n);i++)if(!a[i])
for(int j=i;j;j-=l(j))//枚举状态i的每一个1
{
f[i][a[l(j)]]=inf;//更新此状态
for(int k=i-l(j);k;k-=l(k))
f[i][a[l(j)]]=min(f[i][a[l(j)]],f[i-l(j)][a[l(k)]]+g[a[l(j)]][a[l(k)]]);
}
for(int i=;i<=n;i++)
ans=min(ans,f[(<<n)-][i]);
printf("%.2f\n",ans);
return ;
}
上一篇:Linux下得到毫秒级时间--C语言实现(转-度娘818)


下一篇:python导入cx_Oracle报错的问题!