In the DP of dynamic programming, is it possible to determine whether a certain state is reached (what is the shortest time)?

Source: Internet
Author: User

Codevs1684 Garbage trap

time limit: 1 sspace limit: 128000 KBtitle level: Golden GoldTitle Description Description

Carmen--a holsteins cow that farmer John Cherished dearly--has fallen into the "trash well." "Trash Well" is where farmers throw rubbish, with a depth of D (2 <= d <= 100) feet.

Carmen tried to pile up the rubbish, and when the heap was as high as the well, she could escape out of the well. In addition, Carmen can maintain her life by eating some rubbish.

Every litter can be used to eat or stack, and to pile up rubbish does not take Carmen's time.

Suppose Carmen knew in advance what time t (0<t<=1000) each garbage dropped, and the height of each garbage heap H (1<=h<=25) and the time F (1<=f<=30) to eat the garbage to sustain life, Ask Carmen to escape the first time outside the well, assuming that Carmen currently has enough energy to last 10 hours, if Carmen did not eat within 10 hours, Carmen will starve to death.

Enter a description Input Description

The first Acts 2 integers, D and g (1 <= g <=), G for the amount of garbage put into the well.

The second to the G+1 line consists of 3 integers: T (0 < T <= 1000), indicating the time that the garbage was dropped into the Well, F (1 <= F <= 30), indicating that the garbage could maintain the time of Carmen's life, and H (1 <= H <= 25), the garbage can Height to be padded.

Output description Output Description

If Carmen can crawl out of the trap, output an integer indicating when the earliest can be crawled out, or how long the output of Carmen can survive.

Sample input Sample Input

20 4

5 4 9

9 3 2

12 6 10

13 1 1

Sample output Sample Output

13

Data range and Tips Data Size & Hint

[Sample Description]

Carmen piled up the first rubbish she received: height=9;

Carmen eats the second litter she receives, extending her life from 10 hours to 13 hours;

Carmen piled up a 3rd litter, height=19;

Carmen piled up a 4th litter, height=20.

#include <iostream>using namespacestd; #include<algorithm>#include<cstdio>#include<cstring>intd,g;#defineN 101structlj{intt,f,h;}; Lj Lj[n];intMaxintAintb) {    if(A&GT;B)returnA; returnb;}intCMP (LJ A, LJ b) {returna.t<b.t;}intMaxh[n],dp[n][n];//F[i][j] Indicates the maximum remaining health value of J after the input of the I garbage, where t=i indicates that all the rubbish before I time can be used//① put this rubbish up: use each state in f[i-1] to update: f[i-1][j]-(two garbage time difference)->f[i][j+ This rubbish height]//② the bowl Geixiang: update with each state in F[i-1]: f[i-1][j]+ (This garbage time)->f[i][j]//each transfer O (D), total transfer of G times//maxh says the maximum height that I can reach in the garbage.voidinput () {scanf ("%d%d",&d,&f);  for(intI=1; i<=g;++i) scanf ("%d%d%d",&lj[i].t,&lj[i].f,&lj[i].h); Sort (LJ+1, lj+g+1, CMP);//sort the rubbish in chronological order first}voiddp () {memset (DP,-1,sizeof(DP)); dp[0][0]=Ten; lj[0].t=0; maxh[0]=0; inti,j;  for(i=1; i<=g;++i) {BOOLSur=false;//whether the death sign        intNowh=lj[i].h; intnowf=lj[i].f; intdt=lj[i].t-lj[i-1].t;//Time Difference         for(j=0; j<=maxh[i-1];++J)//maximum height at which the previous point can be cycled        {            if(dp[i-1][j]-dt>=0)/*pile up the rubbish, the transfer equation always takes the maximum value of life,*/{dp[i][j+nowh]=max (dp[i][j+nowh],dp[i-1][j]-DT); Sur=true; Maxh[i]=max (maxh[i],j+NOWH); }        }         for(j=0; j<=maxh[i-1];++j) {if(dp[i-1][j]-dt>=0)/*eat the rubbish .*/{Dp[i][j]=max (dp[i][j],dp[i-1][j]-dt+NOWF); Sur=true; Maxh[i]=Max (MAXH[I],J); }        }        if(!sur)//Death             Break; if(Maxh[i]>=d)//out of the pit{printf ("%d\n", lj[i].t); return ; }    }    intsum=0;  for(j=1; j<i;++j) Sum+=LJ[J].F;//the longest life, pay attention to the first I garbage did not eatprintf"%d\n", sum+Ten);}intMain () {input ();    DP ();    GetChar (); return 0; }
View Code

2.lojs [NOIP2007] catcher's escape

★☆ input file: escape.in output file: escape.out Simple comparison
Time limit: 1 s memory limit: MB

The Watchman's escape

"Problem description"

The Demon Hunter, Dean, is ambitious. He betrayed the night elves, and the rate of the deep seabed was attempted mutiny: The watchman was killed in a clash with Illidan. Trapped on a deserted Big island. In order to kill the Watchman, Dean began to curse the island, the island will soon sink, until then, all the island will be killed: The watchman's running speed, for 17m/s, at such a speed is unable to escape the desert island. Fortunately, the watcher has a flashing spell that can move 60m within 1s, but each use of a flashing spell consumes a mana value of 10 points. The Catcher's Mana recovery rate is 4 points/s, which can only be restored when in-situ resting state.

Now known as the Watcher's magical initial value m, the distance between his initial position and the island's exit S, the island sank time t. Your task is to write a program to help the watcher calculate how to escape from the desert island in the shortest time, if not escape, then the output watcher in the remaining time to walk the farthest distance. Note: The watcher's running, flashing, or resting activities are in seconds (s). and the duration of each activity is an integer second. The unit of distance is in meters (m).

Input

The input file escape.in only one line, including three non-negative integer m,s,t separated by spaces.

Output

The output file Escape.out contains two lines:

The 1th behavior string "Yes" or "No" (case-sensitive), that is, whether the watcher can escape from the desert island.

Line 2nd contains an integer, the first Behavior "Yes" (case-sensitive) indicates the shortest time to flee the desert island

The first behavior "No" (case-sensitive) indicates the maximum distance the watcher can walk.

"Input and output Example 1"

Escape.in

39 200 4

Escape.out

No

197

"Input and output Example 2"

Escape.in

36 255 10

Escape.out

Yes

6

Limit

30% of data meet: 1 <= t<=, 1 <=s<= 100

50% data satisfies: 1 <= T <=, 1 <= S <= 10000

100% data: 1 <= T <= 300000, 0 <= m<=1000 1 <=s <= 10^8

To do with pure greed,
Greedy strategy: (note that the order cannot be reversed!) )

1: If there is magic, first put the law.

2: If the remaining distance is >=120, wait 5s, flash two times. Total time-consuming 7S, forward 120 meters, than Run 7s go far (7x17=119) that is, to use the time tt=tt+7; walking distance is ss:ss=ss+120; when Tt=t-7, Ss<s, m>=2, you should abandon this strategy. (because at this point, we can wait for 1 seconds or 2 seconds to flash.) If t-tt<7, discard this policy.

3: if (s-ss>=34) and (m>=6) and (t-tt>=2), then choose to blink, wait a second, flash a second. Magic minus 6.

4: if (s-ss>=51) and (m>=2) and (t-tt>=3), then choose to blink, wait two seconds, Flash one second, magic minus 2.

5: If the above results can not run out, then choose to flee it. Finally judge whether to run out.

/*the obvious greedy strategy: the classification is more troublesome*/#include<iostream>using namespacestd; #include<cstdio>intm,s,t;ints1,t1;intMain () {scanf ("%d%d%d",&m,&s,&t); S1=0; t1=0;  while(m>=Ten&&t-t1>0&&s-s1>0)/*I'm going to run out of magic.*/   {       if(s-s1> -) {m-=Ten; S1+= -; T1++; }       Else{printf ("yes\n%d\n", t-t1-1);/*Use the magic to determine whether to run out*/           return 0; }   }    while(s-s1>0&&t-t1>0)/*four kinds of escape situations*/   {       if(m<2&&m>=0&&t-t1>=7&&s-s1>119) {S1+= -*2; T1+=7; }       Else if(m>=2&&m<=5&&t-t1>=3&&s-s1> -*3) {T1+=3; M-=2; S1+= -; }       Else if(m>=6&&t-t1>=2&&s-s1> -*2) {T1+=2; S1+= -; M-=6; }       Else{S1+= -; T1++; }   }   if(s1>=s) printf ("yes\n%d\n", T1);/*finally judging whether to run out*/   Elseprintf"no\n%d\n", S1); return 0;}
View Code

In the DP of dynamic programming, is it possible to determine whether a certain state is reached (what is the shortest time)?

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.