The full direction graph of n points. Edge right: |XI-XJ|+CI+BJ (j<i) |xi-xj|+di+aj (j>i)
To start from S, after each point just once to reach the shortest path T? n<=5000
After pressing the above direct edge, it becomes the shortest Hamilton Road???
The value of the edge is not arbitrary, and the distance of the two points and its corresponding a,b,c,d, the accumulation of the edges and split into each single point of contribution after the sum.
A node can have 4 states: In the path, the pre is left/right, and the NXT is left/right. Knowing the state of the point will be able to know the contribution in the path.
Set F[i][j][k] State: The first I point, J on the side to be determined, k out side to be determined. (I,J) can be determined K, memory can be optimized to lose one dimension. Legal transfer can be.
#include <bits/stdc++.h> using namespace std;
typedef long Long LL;
const int n=5e3+5;
const LL INF=2E16;
int n,s,t,x[n],a[n],b[n],c[n],d[n];
ll Dp[n][n];
ll solve (int i,int j,int k) {if (i==n) {if (j==0&&k==0) return dp[i][j]=0;
return INF;
} if (j==0&&k==0&&i) return inf;
if (dp[i][j]!=-1) return dp[i][j];
ll &res=dp[i][j];
Res=inf;
if (i==s) {if (j) res=min (Res,solve (i+1,j-1,k) +x[i]+c[i]);//NXT is some left.
Res=min (Res,solve (i+1,j,k+1)-x[i]+d[i]);//NXT is some right return res;
} if (i==t) {if (k) res=min (Res,solve (i+1,j,k-1) +x[i]+a[i]);
Res=min (Res,solve (i+1,j+1,k)-x[i]+b[i]);//p,r return res;
} res=min (Res,solve (i+1,j+1,k+1) -2*x[i]+b[i]+d[i]);
if (k&&j) res=min (Res,solve (i+1,j-1,k-1) +2*x[i]+a[i]+c[i]);
if (k) res=min (Res,solve (i+1,j,k) +d[i]-x[i]+x[i]+a[i]);
if (j) res=min (Res,solve (i+1,j,k) +c[i]+b[i]);
return res; int main () {while (cin>>n>>s>>t) {memset (dp,-1,sizeof (DP));
s--, t--;
for (int i=0;i<n;i++) scanf ("%d", &x[i]);
for (int i=0;i<n;i++) scanf ("%d", &a[i]);
for (int i=0;i<n;i++) scanf ("%d", &b[i]);
for (int i=0;i<n;i++) scanf ("%d", &c[i]);
for (int i=0;i<n;i++) scanf ("%d", &d[i]);
printf ("%i64d\n", Solve (0,0,0));
return 0;
}