1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <bits/stdc++.h> using namespace std; const int N = 5e3 + 5; typedef long long ll; template<typename T> inline void ckmin(T &x,const T &y) { if(x > y) x = y;} ll f[2][N]; int n,s,t; int X[N],a[N],b[N],c[N],d[N]; int main() { cin >> n >> s >> t; for(int i = 1;i <= n;i++) cin >> X[i]; for(int i = 1;i <= n;i++) cin >> a[i],a[i] += X[i]; for(int i = 1;i <= n;i++) cin >> b[i],b[i] -= X[i]; for(int i = 1;i <= n;i++) cin >> c[i],c[i] += X[i]; for(int i = 1;i <= n;i++) cin >> d[i],d[i] -= X[i]; memset(f,0x3f,sizeof f); f[0][0] = 0; for(int i = 1;i <= n;i++) { memset(f[i&1],0x3f,sizeof f[i&1]); if(i != s && i != t) for(int j = 0;j < i;j++) { ll val = f[(i-1)&1][j]; if((i < max(s,t)) || j > 1) ckmin(f[i&1][j + 1],val + b[i] + d[i]); if(j > 1) ckmin(f[i&1][j - 1],val + a[i] + c[i]); if(j > 0 && (j > 1 || i < s)) ckmin(f[i&1][j],val + c[i] + b[i]); if(j > 0 && (j > 1 || i < t)) ckmin(f[i&1][j],val + a[i] + d[i]); } if(i == s) for(int j = 0;j < i;j++) { ll val = f[(i-1)&1][j]; ckmin(f[i&1][j+1],val + d[i]); if(j > 0) ckmin(f[i&1][j],val + c[i]); } if(i == t) for(int j = 0;j < i;j++) { ll val = f[(i-1)&1][j]; ckmin(f[i&1][j+1],val + b[i]); if(j > 0) ckmin(f[i&1][j],val + a[i]); } } cout << f[n&1][1] << endl; return 0; }
|