POJ 1682 DP
原创:
http://www.cnblogs.com/proverbs/archive/2012/10/03/2711151.html
超高仿:
http://blog.csdn.net/mars_ch/article/details/53020127
高仿:
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 105
#define mem(x) memset(x,0x1f,sizeof(x))
int cases,n,m,p,a[N],b[N],c[N],ans;
int ab[N][N],ac[N][N],bc[N][N];
int main(){
scanf("%d",&cases);
while(cases--){
mem(ab),mem(ac),mem(bc),ans=0x3fffffff;
scanf("%d%d%d",&n,&m,&p);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++)scanf("%d",&b[i]);
for(int i=1;i<=p;i++)scanf("%d",&c[i]);
ab[n+1][0]=ac[0][p+1]=bc[m+1][0]=0;
for(int i=n;i;i--)
for(int j=1;j<=m;j++)
ab[i][j]=min(ab[i+1][j],min(ab[i+1][j-1],ab[i][j-1]))+abs(a[i]-b[j]);
for(int i=1;i<=n;i++)
for(int j=p;j;j--)
ac[i][j]=min(ac[i-1][j],min(ac[i-1][j+1],ac[i][j+1]))+abs(a[i]-c[j]);
for(int i=m;i;i--)
for(int j=1;j<=p;j++)
bc[i][j]=min(bc[i+1][j-1],min(bc[i][j-1],bc[i+1][j]))+abs(b[i]-c[j]);
for(int i=0;i<=n+1;i++)
for(int j=0;j<=m+1;j++)
for(int k=0;k<=p+1;k++){
ans=min(ans,ab[i][j]+bc[j][k]+ac[i][k]);
ans=min(ans,ab[i+1][j]+bc[j][k]+ac[i][k]);
ans=min(ans,ab[i][j]+bc[j+1][k]+ac[i][k]);
ans=min(ans,ab[i][j]+bc[j][k]+ac[i][k+1]);
ans=min(ans,ab[i][j]+bc[j+1][k]+ac[i][k+1]);
ans=min(ans,ab[i+1][j]+bc[j][k]+ac[i][k+1]);
ans=min(ans,ab[i+1][j]+bc[j+1][k]+ac[i][k]);
ans=min(ans,ab[i+1][j]+bc[j+1][k]+ac[i][k+1]);
}
printf("%d\n",ans);
}
}