(Dynamic regulation or shortest path) help Jimmy (POJ 1661)

Source: Internet
Author: User

http://poj.org/problem?id=1661

Description

"Help Jimmy" is a game done on the scene shown.

The scene includes multiple platforms with different lengths and heights. The ground is the lowest platform, the height is zero, the length is infinite.

Jimmy Mouse at the moment 0 begins to fall from somewhere above all platforms, its falling speed is always 1 m/s. When Jimmy Falls on a platform, the player chooses to run it to the left or right, and it runs at a speed of 1 m/s. When Jimmy ran to the edge of the platform, he began to fall. Jimmy never drops more than max meters, or he will fall dead, and the game will end.

Design a program to calculate the earliest possible time for Jimmy to ground.

Input

the first line is the number of groups of test data T (0 <= T <= 20). The first line of each set of test data is four integer n,x,y,max, separated by a space. n is the number of platforms (excluding the ground), X and y are the coordinates of the position where Jimmy begins to fall, and Max is the maximum height of a drop. The next n rows each line describes a platform, including three integers, x1[i],x2[i] and h[i]. H[i] Indicates the height of the platform, X1[i] and x2[i] represent the horizontal axis of the platform's left and right endpoints. 1 <= N <= 1000,-20000 <= X, X1[i], X2[i] <= 20000,0 < H[i] < Y <= 20000 (i = 1..N). The units of all coordinates are meters.

Jimmy's size and platform thickness are negligible. If Jimmy happens to be on the edge of a platform, it is seen as falling on the platform. All platforms are non-overlapping or connected. There must be a solution to the test data guarantee problem.

Output

for each set of test data entered, output an integer that is the earliest possible time for Jimmy to ground.

Sample Input

13 8 17 200 10 80 10 134 14 3

Sample Output

23

Reverse thinking, if from the top down, state conversion, the current point may have a lot of points to arrive, but if the reverse, it is much simpler, the current point can only be reached by 2 points

Dynamic regulation:

#include <iostream>#include<cstdio>#include<algorithm>using namespacestd;Const intN =1010;Const intINF =0x3fffffff;structnode{intL, R, H; BOOL operator< (ConstNode &n1)Const    {        returnH <N1.    H }} A[n];intdp[n][2];///Dp[i][0] Represents the minimum time required to reach the platform from the left to the first///Dp[i][1] Represents the minimum time required to reach the platform from the right to the firstintSlove (intNintMax) {    intI, J, H;  for(i=1; i<n; i++)    {        ///so I'm going to pour it backwards because if there's any, it's going to get blocked, not directly .///so finding the one that meets the conditions will end.         for(j=i-1; j>=0; j--)          {            ///determine if I platform can reach the left side of J Platform            if(A[i]. L&GT;=A[J]. L && A[i]. l<=A[j]. R) {h= A[i]. HA[j].                H if(H>max) dp[i][0] =INF; Else if(j==0) dp[i][0] = h;///J platform from left endpoint to platform I and from right endpoint to platform I take the minimum value                Elsedp[i][0] = min (dp[j][0]+a[i]. L-A[J]. L, dp[j][1]+A[J]. R-a[i]. L) +h;  Break; }        }         for(j=i-1; j>=0; j--)        {            ///determine if I platform can reach the right side of J platform            if(A[i]. R&GT;=A[J]. L && A[i]. r<=A[j]. R) {h= A[i]. HA[j].                H if(H>max) dp[i][1] =INF; Else if(j==0) dp[i][1] =h; Elsedp[i][1] = min (dp[j][0]+a[i]. R-A[J]. L, dp[j][1]+A[J]. R-a[i]. R) +I;  Break; }        }    }    returndp[n-1][0];}intMain () {intT; scanf ("%d", &T);  while(t--)    {        intI, N, X, Y, Max; scanf ("%d%d%d%d", &n, &x, &y, &Max);  for(i=1; i<=n; i++) {scanf ("%d%d%d", &a[i]. L, &a[i]. R, &A[i].            H); if(A[i]. L>A[i]. R) Swap (A[i]. L, A[i].        R); } a[0]. L=x, a[0]. R=x, a[0]. H=x; A[n+1]. l=-20005, a[n+1]. R=20005, a[n+1]. H=0; N+=2; Sort (A, a+N); printf ("%d\n", Slove (n, Max)); }    return 0;}

(Dynamic regulation or shortest path) help Jimmy (POJ 1661)

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.