codeforces 366 Ant Man dp

Ant Man

题目连接:

(http://codeforces.com/contest/704/problem/B)

Description

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 x, a, b, c, d 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.

Sample Input

7 4 3
8 11 12 16 17 18 20
17 16 20 2 20 5 13
17 8 8 16 12 15 13
12 4 16 4 15 7 6
8 14 2 11 17 12 8

Sample Output

139

Hint

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

题意

从s到e,每个点经过一次,求最小代价

题解:

路径代价分解后得到,从这个点向左向右,从左从右到这个点的代价
然后dp,dp[len][a][b]表示前len个点,入度为a,出度为b的最优值。
因为ab最多相差1,复杂度n*n
说的很简单,但这个dp剧毒:
1.用每个点的入度和出度来避免特判s,e
2.前x个点,入度出度都为0的情况是非法的
3.更新是要考虑入度为0,但入度-1 +1之后为0的情况,也是非法

代码

//#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <time.h>
using namespace std;
long double esp=1e-11;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define cle(a) while(!a.empty())a.pop()
#define mem(p,c) memset(p,c,sizeof(p))
#define mp(A, B) make_pair(A, B)
#define pb push_back
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
typedef long long int LL;
const long double PI = acos((long double)-1);
const LL INF=0x3f3f3f3f3f3f3f3fll;
const LL MOD =1000000007ll;
const int maxn=5001;
LL dp[2][maxn][3];
LL m[maxn][4];
int in[maxn][2],n;
void update(int x,int a,int b,LL ans)
{
	if(a==0&&b==0&&x!=n)return;
	if(a>=0&&b>=0&&(a-b+1)>=0&&(a-b+1)<3)
	{
		dp[x&1][a][a-b+1]=min(dp[x&1][a][a-b+1],ans);
		//printf("****%d %d %d %lld\n",x,a,b,ans);
	}
}
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("inlay.in", "r", stdin);
    //freopen("out.txt", "w", stdout);
    //::iterator iter;                  %I64d
    //for(int x=1;x<=n;x++)
    //for(int y=1;y<=n;y++)
    //scanf("%d",&a);
    //printf("%d\n",ans);
    int s,e;
    scanf("%d%d%d",&n,&s,&e);
    LL t;
    for(int x=1;x<=n;x++)
	{
		scanf("%I64d",&t);
		m[x][0]=m[x][2]=t;
		m[x][1]=m[x][3]=-t;
		in[x][0]=1;
		in[x][1]=1;
	}
	for(int y=0;y<4;y++)
		for(int x=1;x<=n;x++)
		{
			scanf("%I64d",&t);
			m[x][y]+=t;		//printf("%d %d %lld\n",x,y,m[x][y]);
		}
	m[s][0]=m[s][1]=0;
	m[e][2]=m[e][3]=0;
	in[s][0]=0;
	in[s][1]=1;
	in[e][0]=1;
	in[e][1]=0;
	memset(dp,0x3f,sizeof(dp));
	LL a,b;
	dp[0][0][1]=0;
	for(int x=1;x<=n;x++)
	{
		mem(dp[x&1],0x3f);
		for(int y=0;y<=n;y++)
		for(int z=0;z<3;z++)
		if(dp[(x-1)&1][y][z]!=INF)
		{
			a=y;
			b=a+1-z;
			if(b<0)continue;
			//printf("%d %d %d %lld\n",x-1,a,b,dp[(x-1)&1][y][z]);
			if(a-in[x][1]>=0)
			update(x,a-in[x][1]+in[x][0],b,dp[(x-1)&1][y][z]+m[x][1]+m[x][2]);
			if(b-in[x][0]>=0)
			update(x,a,b-in[x][0]+in[x][1],dp[(x-1)&1][y][z]+m[x][0]+m[x][3]);
			update(x,a+in[x][0],b+in[x][1],dp[(x-1)&1][y][z]+m[x][1]+m[x][3]);
			if(a-in[x][1]>=0&&b-in[x][0]>=0)
			update(x,a-in[x][1],b-in[x][0],dp[(x-1)&1][y][z]+m[x][0]+m[x][2]);
		}
	}
	printf("%I64d\n",dp[n&1][0][1]);
    return 0;
}
posted @ 2016-08-08 19:35  femsub  阅读(385)  评论(0编辑  收藏  举报