Codeforces Round #366 (Div. 2) [D] Ant Man

来源:互联网 发布:菲律宾4g网络制式 编辑:程序博客网 时间:2024/05/16 07:48
Ant Man
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Scott Lang is at war with Darren Cross. There are n chairs in a hall where they are, numbered with 1, 2, ..., n from left to right. The i-th chair is located at coordinate xi. Scott is on chair number s and Cross is on chair number e. Scott can jump to all other chairs (not only neighboring chairs). He wants to start at his position (chair number s), visit each chair exactly once and end up on chair number e with Cross.

As we all know, Scott can shrink or grow big (grow big only to his normal size), so at any moment of time he can be either small or large (normal). The thing is, he can only shrink or grow big while being on a chair (not in the air while jumping to another chair). Jumping takes time, but shrinking and growing big takes no time. Jumping from chair number i to chair number j takes |xi - xj| seconds. Also, jumping off a chair and landing on a chair takes extra amount of time.

If Scott wants to jump to a chair on his left, he can only be small, and if he wants to jump to a chair on his right he should be large.

Jumping off the i-th chair takes:

  • ci extra seconds if he's small.
  • di extra seconds otherwise (he's large).

Also, landing on i-th chair takes:

  • bi extra seconds if he's small.
  • ai extra seconds otherwise (he's large).

In simpler words, jumping from i-th chair to j-th chair takes exactly:

  • |xi - xj| + ci + bj seconds if j < i.
  • |xi - xj| + di + aj seconds otherwise (j > i).

Given values of xabcd find the minimum time Scott can get to Cross, assuming he wants to visit each chair exactly once.

Input

The first line of the input contains three integers n, s and e (2 ≤ n ≤ 5000, 1 ≤ s, e ≤ n, s ≠ e) — the total number of chairs, starting and ending positions of Scott.

The second line contains n integers x1, x2, ..., xn (1 ≤ x1 < x2 < ... < xn ≤ 109).

The third line contains n integers a1, a2, ..., an (1 ≤ a1, a2, ..., an ≤ 109).

The fourth line contains n integers b1, b2, ..., bn (1 ≤ b1, b2, ..., bn ≤ 109).

The fifth line contains n integers c1, c2, ..., cn (1 ≤ c1, c2, ..., cn ≤ 109).

The sixth line contains n integers d1, d2, ..., dn (1 ≤ d1, d2, ..., dn ≤ 109).

Output

Print the minimum amount of time Scott needs to get to the Cross while visiting each chair exactly once.

Example
input
7 4 38 11 12 16 17 18 2017 16 20 2 20 5 1317 8 8 16 12 15 1312 4 16 4 15 7 68 14 2 11 17 12 8
output
139
Note

In the sample testcase, an optimal solution would be . Spent time would be17 + 24 + 23 + 20 + 33 + 22 = 139.


题意:在一个数轴上有n个点,每个点有5个值x,a,b,c,d,你每次可以从一个点i跳跃到另外一个点j,如果j在i的右则需要花费abs(i-j)+d[i]+a[j],如果j在i的左边,则需要花费abs(i-j)+c[i]+b[j],一开始你位于s点,你需要走过所有的点并且最后停在e点,求最小花费(每个点最多走1遍)


这题表面上是dp,实际上贪心也能过,确实是个比较显然的贪心:在一条链上先设置两个点s,e,并且s→e,然后枚举每个点并在这条链上逐个添加增加代价最小的点,就完事了

复杂度O(n^2)

#include<cstdio>#include<iostream>#include<cstdlib>#define LL long longusing namespace std;const LL maxn=5005,inf=1e18;inline void _read(LL &x){    char t=getchar();bool sign=true;    while(t<'0'||t>'9')    {if(t=='-')sign=false;t=getchar();}    for(x=0;t>='0'&&t<='9';t=getchar())x=x*10+t-'0';    if(!sign)x=-x;}LL NEXT[maxn],s,e,n,sum;LL x[maxn],a[maxn],b[maxn],c[maxn],d[maxn];LL dis(LL g, LL v)  {      return abs(x[g]-x[v])+ (x[v]<x[g]?c[g]+b[v]:d[g]+a[v]); }  int main(){_read(n);_read(s);_read(e);LL i,j;for(i=1;i<=n;i++)_read(x[i]);for(i=1;i<=n;i++)_read(a[i]);for(i=1;i<=n;i++)_read(b[i]);for(i=1;i<=n;i++)_read(c[i]);for(i=1;i<=n;i++)_read(d[i]);NEXT[s]=e;sum+=dis(s,e); for(i=1;i<=n;i++){if(i==s||i==e)continue;LL maxx=inf,ans,temp;for(j=s;j!=e;j=NEXT[j]){temp=dis(j,i)+dis(i,NEXT[j])-dis(j,NEXT[j]);if(temp<maxx)maxx=temp,ans=j; }sum+=maxx;NEXT[i]=NEXT[ans];NEXT[ans]=i;}cout<<sum;} 


0 0
原创粉丝点击