三个河岸两两之间dp一次,再和起来:
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int NN=110; const int INF=0xf0f0f0f; int n,m,k,x[NN],y[NN],z[NN],dp1[NN][NN],dp2[NN][NN],dp3[NN][NN]; inline int Abs(int x) { if (x>=0) return x; else return -x; } int main() { int cas,ans,tmp,tmp1,tmp2; scanf("%d",&cas); while (cas--) { scanf("%d%d%d",&n,&m,&k); for (int i=1; i<=n; i++) scanf("%d",&x[i]); for (int i=1; i<=m; i++) scanf("%d",&y[i]); for (int i=k; i>=1; i--) scanf("%d",&z[i]); memset(dp1,0xf,sizeof(dp1)); memset(dp2,0xf,sizeof(dp2)); memset(dp3,0xf,sizeof(dp3)); //dp1 dp1[0][0]=0; for (int i=1; i<=n; i++) for (int j=1; j<=k; j++) { dp1[i][j]=min(dp1[i-1][j-1],min(dp1[i-1][j],dp1[i][j-1]))+Abs(x[i]-z[j]); //printf("dp1 %d %d : %d\n",i,j,dp1[i][j]); } //dp2 dp2[m+1][k+1]=0; for (int i=m; i>=1; i--) for (int j=k; j>=1; j--) { dp2[i][j]=min(dp2[i+1][j+1],min(dp2[i+1][j],dp2[i][j+1]))+Abs(y[i]-z[j]); //printf("dp2 %d %d : %d\n",i,j,dp2[i][j]); } //dp3 dp3[n+1][0]=0; for (int i=n; i>=1; i--) for (int j=1; j<=m; j++) { dp3[i][j]=min(dp3[i+1][j-1],min(dp3[i+1][j],dp3[i][j-1]))+Abs(x[i]-y[j]); //printf("dp3 %d %d : %d\n",i,j,dp3[i][j]); } //get ans ans=INF; for (int i=0; i<=n; i++) for (int j=0; j<=m; j++) for (int l=0; l<=k; l++) { tmp1=min(min(dp2[j][l],dp2[j+1][l]),min(dp2[j][l+1],dp2[j+1][l+1])); tmp2=min(dp3[i][j],dp3[i+1][j]); //WA了几次,主要是刚开始的写法不自觉的把点严格分块了, //事实是一个点又不是不能同时连多个点, //这样就连像"1 1 1 1 2 3"这样的数据都过不了 tmp=dp1[i][l]+tmp1+tmp2; if (tmp<ans) ans=tmp; } printf("%d\n",ans); } return 0; }