Looking for Order题解

Looking for Order题解

倒是一道简单题,
状压dp,
没什么说的,

#include<bits/stdc++.h>
using namespace std;
const int N=17e6+7,M=26;
int n,p,f[N],dis[M][M],x[M],y[M],inf,w,t,pre[N],q;
inline int read(){
   int T=0,F=1; char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-') F=-1; ch=getchar();}
   while(ch>='0'&&ch<='9') T=(T<<3)+(T<<1)+(ch-48),ch=getchar();
   return F*T;
}
int main(){
    memset(f,0x3f,sizeof(f)),inf=f[0],f[0]=0,x[0]=read(),y[0]=read(),n=read(),p=(1<<n);
    for(int i=1;i<=n;++i) x[i]=read(),y[i]=read();
    for(int i=0;i<=n;++i) for(int j=0;j<=n;++j) dis[i][j]=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);//,cout<<i<<" "<<j<<" "<<dis[i][j]<<endl;
    for(int i=0;i<p;++i){
        if(f[i]==inf) continue;
        for(int j=0;j<n;++j){
            if(i&(1<<j)) continue;
            for(int k=0;k<n;++k){
                if(i&(1<<k)) continue;
                w=dis[0][j+1]+dis[j+1][k+1]+dis[k+1][0],t=(i|(1<<j)|(1<<k));
                if(f[t]>f[i]+w) f[t]=f[i]+w,pre[t]=i;
            }
            break;
        }
    }
    printf("%d\n",f[p-1]),--p;
    while(p){
      printf("0 "),w=(p^pre[p]),t=(int)log2(w),p=pre[p];
      if(w!=(1<<t)) q=log2(w-(1<<t)),printf("%d %d ",t+1,q+1);
      else printf("%d ",t+1);
    }
    printf("0\n");
    return 0;
}
posted @ 2019-10-14 22:17  lsoi_ljk123  阅读(151)  评论(0编辑  收藏  举报