The meaning of problems
Predetermined start point, end point.
Through all the points, from a point a little to the left to jump to different contribution to the right. And one can only access point and only once.
answer
A practice:
, we can enter a predetermined point of each edge, the edge, the transfer equation can be found.
But suffer from not understand the special circumstances of the starting and ending points. Temporarily provide solution to a problem.
Practice two: Piggy
start to finish
entered, one by one insertion point, each insert when the old contribution remove plus new contributions.
Then find the best position.
This is revocable greedy, so correct.
I can write a list.
#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define sf(x) scanf("%d",&x)
using namespace std;
typedef long long ll;
const int maxn = 350050;
int x[maxn],a[maxn],b[maxn],c[maxn],d[maxn];
int nxt[5050];
ll dist(int i,int j){
if(i>j)return abs(x[i]-x[j])+1ll*c[i]+1ll*b[j];
else return abs(x[i]-x[j])+1ll*d[i]+1ll*a[j];
}
void insert(int x,int y){
nxt[y]=nxt[x];
nxt[x]=y;
}
int main(){
int n,s,e;cin>>n>>s>>e;
FOR(i,1,n)sf(x[i]);
FOR(i,1,n)sf(a[i]);
FOR(i,1,n)sf(b[i]);
FOR(i,1,n)sf(c[i]);
FOR(i,1,n)sf(d[i]);
int l=s,r=e;
nxt[s]=e;
nxt[e]=-1;
ll ans=dist(s,e);
for(int i=1;i<=n;i++)if(i!=s&&i!=e){
ll Mix=0x3f3f3f3f3f3f3f3f,id;
for(int j=s;nxt[j]!=-1;j=nxt[j]){
ll tmp=dist(j,i)+dist(i,nxt[j])-dist(j,nxt[j]);
if(tmp<Mix){
Mix=tmp;
id=j;
}
}
ans+=Mix;
insert(id,i);
}
cout<<ans<<endl;
}