[DP | violence] POJ-1661 Help Jimmy, dppoj-1661

Source: Internet
Author: User

[DP | violence] POJ-1661 Help Jimmy, dppoj-1661

Help Jimmy
Time Limit: 1000 MS Memory Limit: 10000 K

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

1
3 8 17 20
0 10 8
0 10 13
4 14 3

Sample Output

23

Source
POJ Monthly-2004.05.15 CEOI 2000

Ideas:The idea is too violent to come up...
Reference: AC_Von
Dp [I] [2] is sufficient to indicate the status, indicating the shortest path to reach the vertices at both ends of platform I.
The rest is enumeration...
Note that the ground is not used as a platform for calculation, so a layer should be added to the ground for judgment.
The Code is as follows:

/** ID: j. sure.1 * PROG: * LANG: C ++ */# include <cstdio> # include <cstdlib> # include <cstring> # include <algorithm> # include <ctime> # include <cmath> # include <stack> # include <queue> # include <vector> # include <map> # include <set> # include <string> # include <iostream> # define PB push_back # define LL long longusing namespace std; const int INF = 0x3f3f3f; const double eps = 1e-8; /******************************* * *******/Const int N = 1005; int n, lim, sx, sy; struct Node {int x [2], h; bool operator <(const Node & rhs) const {return h> rhs. h ;}} a [N]; int dp [N] [2]; // you can only go left or right to int solve () {memset (dp, 0x3f, sizeof (dp); dp [0] [0] = dp [0] [1] = 0; for (int I = 1; I <= n; I ++) {for (int j = 0; j <I; j ++) {for (int d = 0; d <2; d ++) {if (a [I]. x [0] <= a [j]. x [d] & a [I]. x [1]> = a [j]. x [d]) {if (a [j]. h-a [I]. h <= Lim) {bool OK = true; for (int k = j + 1; k <I; k ++) {if (a [k]. x [0] <= a [j]. x [d] & a [k]. x [1]> = a [j]. x [d]) {OK = false; break ;}// no barrier in the middle if (OK & dp [j] [d]! = INF) {int tmp = dp [j] [d] + a [j]. h-a [I]. h + abs (a [j]. x [d]-a [I]. x [0]); dp [I] [0] = min (dp [I] [0], tmp ); tmp = dp [j] [d] + a [j]. h-a [I]. h + abs (a [j]. x [d]-a [I]. x [1]); dp [I] [1] = min (dp [I] [1], tmp );}}}}}} printf ("dp [n] [0] is % d, dp [n] [1] is % d \ n", dp [n] [0], dp [n] [1]); // The dp array stores the minimum short-circuit int ans = INF; for (int I = 0; I <= n; I ++) {if (a [I]. h <= lim) {for (int d = 0; d <2; d ++) {bool OK = true; for (int k = I + 1; k <= n; k ++) {if (a [k]. x [0] <= a [I]. x [d] & a [k]. x [1]> = a [I]. x [d]) {OK = false; break ;}} if (OK) {ans = min (ans, dp [I] [d] + a [I]. h) ;}}}return ans;} int main () {# ifdef J_Sure freopen ("000.in"," r ", stdin); // freopen (" 999.out ", "w", stdout); # endif int T; scanf ("% d", & T); while (T --) {scanf ("% d", & n, & sx, & sy, & lim); a [0]. x [0] = a [0]. x [1] = sx; a [0]. h = sy; for (int I = 1; I <= n; I ++) {scanf ("% d", & a [I]. x [0], & a [I]. x [1], & a [I]. h);} sort (a, a + n + 1); int ans = solve (); printf ("% d \ n", ans);} 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.