POJ 1661-help JIMMY-DP

Source: Internet
Author: User

A DP simulation problem.

Give n boards and an initial coordinate xy, starting from X, Y, and jumping down, with a maximum hop-down distance.

Both walking and jumping on the board are time consuming. Calculates the shortest time to reach the ground.

Sort the boards by height

Dp[i][0] Indicates the time to reach the left edge of Block I, dp[i][1] indicates the time to reach the right edge.

Then dp[i][0] = min (Dp[k][0]+f (), Dp[k][1]+f ()) k is less than I can jump to I on the board. F () indicates the time required from the left/right edge of K to the left edge of I.

Dp[i][1] = min (Dp[k][0]+f (), dp[k][1]+f ())

Pay attention to determine whether K can jump to I more trouble, K at the end of the coordinates in I, and K has not jumped the board, because one side of the board may only jump to a single board, or the ground.

Finally, the minimum value of the DP value at one end of the board that can jump directly to the ground is maintained.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 7 Const intMAXN = ++Ten;8 Const intINF =0x3f3f3f3f;9 Ten structtable{ One     intL,r; A     intheight; -     BOOL operator< (ConstTable &AMP;RHS)Const -     { the         returnHeight >Rhs.height; -     } - }TABLE[MAXN]; -  + intN,x0,y0,max; - intT; + intdp[maxn][2]; A intvis[maxn],next[maxn][2]; at  - BOOLCheckintIintj) - { -     if(Table[j].height-table[i].height <= Max && table[j].height! =table[i].height) -         return true; -     Else in         return false; - } to  + intMain () - { thescanf"%d",&T); *      while(t--) $     {Panax Notoginsengscanf"%d%d%d%d",&n,&x0,&y0,&Max); -          for(intI=1; i<=n;i++) the         { +scanf"%d%d%d",&table[i].l,&table[i].r,&table[i].height); A             if(Table[i].l >TABLE[I].R) the swap (TABLE[I].L,TABLE[I].R); +         } -  $Sort (table+1, table+1+N); $memset (Vis,0,sizeofvis); -Memset (Next,0,sizeofnext); -  the          for(intI=0; i<=n;i++) -dp[i][0] = dp[i][1] =INF;Wuyi         intAns =INF; the         intFlag =0; -  Wu          for(intI=1; i<=n;i++) -         { About             if(Y0-table[i].height > Max) Break; $             if(X0 >= table[i].l && X0 <=TABLE[I].R) -             { -dp[i][0] = y0-table[i].height + X0-TABLE[I].L; -dp[i][1] = y0-table[i].height + TABLE[I].R-X0; AVis[i] =true; +Flag =1; the                  Break; -             } $         } the  the         if(!flag) the         { theprintf"%d\n", Y0); -             Continue; in         } the  the          for(intI=1; i<=n;i++) About         { the              for(intj=i-1; j>=1; j--)if(Check (I,J) &&Vis[j]) the             { the                 if(!next[j][0] && table[j].l <= table[i].r && table[j].l >=table[i].l) +                 { -next[j][0] =true; theVis[i] =true;Bayidp[i][0] = min (dp[j][0]+table[j].height-table[i].height+table[j].l-table[i].l,dp[i][0]); thedp[i][1] = min (dp[j][0]+table[j].height-table[i].height+table[i].r-table[j].l,dp[i][1]); the                 } -                 if(!next[j][1] && table[j].r >= table[i].l && TABLE[J].R <=TABLE[I].R) -                 { thenext[j][1] =true; theVis[i] =true; thedp[i][0] = min (dp[j][1]+table[j].height-table[i].height+table[j].r-table[i].l,dp[i][0]); thedp[i][1] = min (dp[j][1]+table[j].height-table[i].height+table[i].r-table[j].r,dp[i][1]); -                 } the             } the             //printf ("l:%d r:%d \ n", dp[i][0],dp[i][1]); the         }94  the          for(inti=n;i>=1; i--)if(Vis[i] && table[i].height <=Max) the         { the             if(!next[i][0]) ans = min (ans,dp[i][0]+table[i].height);98             if(!next[i][1]) ans = min (ans,dp[i][1]+table[i].height); About  -             //printf ("h:%d l:%d r:%d [%d,%d] \ n", table[i].height,dp[i][0],dp[i][1],next[i][0],next[i][1]);101         }102printf"%d\n", ans);103     }104}

Recently did a lot of DP water problems, but do not want to upload, too much water.

POJ 1661-help JIMMY-DP

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.