Codeforces 705D Ant man DP (contribution) __codeforces_

Source: Internet
Author: User

The full direction graph of n points. Edge right: |XI-XJ|+CI+BJ (j<i) |xi-xj|+di+aj (j>i)
To start from S, after each point just once to reach the shortest path T? n<=5000
After pressing the above direct edge, it becomes the shortest Hamilton Road???
The value of the edge is not arbitrary, and the distance of the two points and its corresponding a,b,c,d, the accumulation of the edges and split into each single point of contribution after the sum.
A node can have 4 states: In the path, the pre is left/right, and the NXT is left/right. Knowing the state of the point will be able to know the contribution in the path.
Set F[i][j][k] State: The first I point, J on the side to be determined, k out side to be determined. (I,J) can be determined K, memory can be optimized to lose one dimension. Legal transfer can be.

#include <bits/stdc++.h> using namespace std;
typedef long Long LL;
const int n=5e3+5;
const LL INF=2E16;
int n,s,t,x[n],a[n],b[n],c[n],d[n];
ll Dp[n][n];
		ll solve (int i,int j,int k) {if (i==n) {if (j==0&&k==0) return dp[i][j]=0;
	return INF;
	} if (j==0&&k==0&&i) return inf;
	if (dp[i][j]!=-1) return dp[i][j];
	ll &res=dp[i][j];
	Res=inf;
		if (i==s) {if (j) res=min (Res,solve (i+1,j-1,k) +x[i]+c[i]);//NXT is some left.
	Res=min (Res,solve (i+1,j,k+1)-x[i]+d[i]);//NXT is some right return res;
		} if (i==t) {if (k) res=min (Res,solve (i+1,j,k-1) +x[i]+a[i]);
	Res=min (Res,solve (i+1,j+1,k)-x[i]+b[i]);//p,r return res;
	} res=min (Res,solve (i+1,j+1,k+1) -2*x[i]+b[i]+d[i]);
	if (k&&j) res=min (Res,solve (i+1,j-1,k-1) +2*x[i]+a[i]+c[i]);
	if (k) res=min (Res,solve (i+1,j,k) +d[i]-x[i]+x[i]+a[i]);
	if (j) res=min (Res,solve (i+1,j,k) +c[i]+b[i]);	
return res; int main () {while (cin>>n>>s>>t) {memset (dp,-1,sizeof (DP));
		s--, t--;
		for (int i=0;i<n;i++) scanf ("%d", &x[i]);		
		for (int i=0;i<n;i++) scanf ("%d", &a[i]);	
		for (int i=0;i<n;i++) scanf ("%d", &b[i]);	
		for (int i=0;i<n;i++) scanf ("%d", &c[i]);
		for (int i=0;i<n;i++) scanf ("%d", &d[i]);
	printf ("%i64d\n", Solve (0,0,0));
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.