POJ 1661 Help Jimmy (DP, note the boundary), pojdp

Source: Internet
Author: User

POJ 1661 Help Jimmy (DP, note the boundary), pojdp
Help Jimmy

Time Limit:1000 MS   Memory Limit:10000 K
Total Submissions:9399   Accepted:3025

Description

"Help Jimmy" is a game completed in the scenario shown.

The scenario includes multiple platforms with different lengths and heights. The ground is the lowest platform with Zero height and unlimited length.

The Jimmy Mouse starts to fall from somewhere higher than all platforms at the moment, and its whereabouts are always at a speed of 1 meter/second. When Jimmy falls onto a platform, the gamer chooses to make it run left or right, and the speed is also 1 meter/second. When Jimmy ran to the edge of the platform, he began to fall. Jimmy's height cannot exceed MAX meters each time he falls, or the game will end.

Design a program to calculate the earliest possible time when Jimmy was on the ground.

Input

The first line is the number of test data groups t (0 <= t <= 20 ). The first row of each group of test data is four integers N, X, Y, MAX, separated by spaces. N is the number of platforms (not including the ground), X and Y are the horizontal and vertical coordinates of the location where Jimmy starts to fall, and MAX is the maximum height of one fall. The next N rows describe a platform in each row, including three integers, X1 [I], X2 [I], and H [I]. H [I] indicates the height of the platform. X1 [I] and X2 [I] indicate the horizontal coordinates of the left and right endpoints of the platform. 1 <= N <= 1000,-20000 <= X, X1 [I], x2 [I] <= 20000,0 <H [I] <Y <= 20000 (I = 1 .. N ). All coordinates are in meters.

Jimmy's size and platform thickness are ignored. If Jimmy falls on the edge of a platform, it is considered to be on the platform. All platforms are not overlapped or connected. Test data to ensure that the problem is resolved.

Output

Output an integer for each group of input test data, which is the earliest possible time when Jimmy is on the ground.

Sample Input

13 8 17 200 10 80 10 134 14 3

Sample Output

23

Source

POJ Monthly -- 2004.05.15 CEOI 2000


Note: [-], [], the two platforms can jump when the height is allowed .... Pitfall for a long time

# Include <iostream> # include <fstream> # include <string> # include <time. h> # include <vector> # include <map> # include <queue> # include <algorithm> # include <stack> # include <cstring> # include <cmath> # include <set> # include <vector> using namespace std; # define INF 0x7fffffffint n, x, y, maxn; int dp [1111] [2]; // dp [I] [0] indicates the minimum value to reach the left endpoint of the I platform, dp [I] [1] indicates the minimum bool vist of the right endpoint [1111] [2]; struct node {int l; int r; int h;} dian [1111]; int cmp (Const node & a, const node & B) {return. h> B. h;} int main () {// freopen ("G: // test.txt", "r", stdin); int t; scanf ("% d ", & t); while (t --) {scanf ("% d", & n, & x, & y, & maxn); memset (vist, false, sizeof (vist); dian [0]. l = x; dian [0]. r = x; dian [0]. h = y; dp [0] [0] = 0; dp [0] [1] = 0; for (int I = 1; I <= n; ++ I) {scanf ("% d", & dian [I]. l, & dian [I]. r, & dian [I]. h); dp [I] [0] = dp [I] [1] = INF;} sort (dian + 1, dian + n + 1, cmp ); for (int I = 1; I <= n; ++ I) {for (int J = 0; j <I; ++ j) {// The enumerated values are higher than I or equal (if they are equal to T) if (vist [j] [0] = false) {if (dian [j]. h> dian [I]. h & dian [j]. h-dian [I]. h <= maxn & dian [j]. l> = dian [I]. l & dian [j]. l <= dian [I]. r) {vist [j] [0] = true; if (dp [j] [0] = INF) continue; dp [I] [0] = min (dp [I] [0], dp [j] [0] + dian [j]. l-dian [I]. l); dp [I] [1] = min (dp [I] [1], dp [j] [0] + dian [I]. r-dian [j]. l) ;}}// left if (vist [j] [1] = false) {if (dian [j]. h> dian [I]. h & dian [j]. h-dian [I]. h <= maxn & dian [j]. r> = dian [I]. L & dian [j]. r <= dian [I]. r) {vist [j] [1] = true; if (dp [j] [1] = INF) continue; dp [I] [0] = min (dp [I] [0], dp [j] [1] + dian [j]. r-dian [I]. l); dp [I] [1] = min (dp [I] [1], dp [j] [1] + dian [I]. r-dian [j]. r) ;}/// right} // for} // forint ans = INF; for (int I = 0; I <= n; ++ I) {if (vist [I] [0] = false & dian [I]. h <= maxn) ans = min (ans, dp [I] [0]); if (vist [I] [1] = false & dian [I]. h <= maxn) ans = min (ans, dp [I] [1]);} if (ans = INF) {printf ("big brother... If the boundary is greater than or equal to this boundary, I kneel down \ n ");} elseprintf (" % d \ n ", ans + y);} return 0 ;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.