标签:des style blog http color io os ar java
状压DP。。。。dp【i】【j】已经走过的点的状态,目前再j点的最小距离
3 3 0 0 0 0 100 0 0 0 0 2 2 1 1 1 1
4 4
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m,pn;
int a[110][110];
int dp[5000][20];
struct POINT
{
int x,y;
}pt[30];
int dist(int a,int b)
{
POINT A=pt[a],B=pt[b];
return abs(A.x-B.x)+abs(A.y-B.y);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
pn=0;
pt[pn++]=(POINT){0,0};
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
if(i==0&&j==0) continue;
if(a[i][j]) pt[pn++]=(POINT){i,j};
}
}
memset(dp,63,sizeof(dp));
for(int i=0;i<(1<<pn);i++)
{
for(int j=0;j<pn;j++)
{
if(i&(1<<j))
{
if(i==(1<<j)) dp[i][j]=dist(0,j);
else
{
int ii=i^(1<<j);
for(int k=0;k<pn;k++)
{
if(i&(1<<k)&&k!=j)
{
dp[i][j]=min(dp[i][j],dp[ii][k]+dist(k,j));
}
}
}
}
}
}
int ans=INF;
for(int i=0;i<pn;i++)
{
ans=min(ans,dp[(1<<pn)-1][i]+dist(0,i));
}
printf("%d\n",ans);
}
return 0;
}
HDOJ 5067 Harry And Dig Machine 状压DP
标签:des style blog http color io os ar java
原文地址:http://blog.csdn.net/ck_boss/article/details/40381419