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
| #include<bits/stdc++.h> #define ll long long #define fi first #define se second #define pb push_back #define inf 0x3f3f3f3f3f3f3f3f using namespace std; const int N=5005; int n,s,t,to[N]; ll a[N],b[N],c[N],d[N],X[N],res; ll calc(int i,int j){ if(i>j)return X[i]-X[j]+c[i]+b[j]; return X[j]-X[i]+d[i]+a[j]; } int main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n>>s>>t; for(int i=1;i<=n;i++)cin>>X[i]; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<=n;i++)cin>>b[i]; for(int i=1;i<=n;i++)cin>>c[i]; for(int i=1;i<=n;i++)cin>>d[i]; to[s]=t; for(int i=1;i<=n;i++){ if(i==s||i==t)continue; pair<ll,int>tmp={inf,0}; for(int j=s;j!=t;j=to[j]){ tmp=min(tmp,{calc(j,i)+calc(i,to[j])-calc(j,to[j]),j}); }to[i]=to[tmp.se],to[tmp.se]=i; }for(int i=s;i!=t;i=to[i])res+=calc(i,to[i]); cout<<res; }
|