UVA 1025
1#include <cstdio>2#include <iostream>3#include <queue>4#include <vector>5#include <stack>6#include <Set>7#include <string>8#include <cstring>9#include <math.h>Ten#include <algorithm> One#include <map> A -#include <sstream> -#include <ctype.h> the - - #defineLson l,m,rt<<1 - #defineRson m+1,r,rt<<1|1 + -typedefLong Longll; + using namespacestd; A at Const intINF =0x7f7f7f7f; - Const DoublePI = ACOs (-1.0); - Const intMAXN =100005; - #defineLOCAL 0 - #defineMOD 1000000007 - in - intn,t; to intL,r; + intls[ -],rs[ -]; - inttimecost[ -]; the intdp[ About][ -]; * BOOLhas_train[ About][ -][2]; $ intKase =1;Panax Notoginseng - voidInput_pre () { theMemset (DP,0,sizeof(DP)); +memset (Timecost,0,sizeof(Timecost)); Amemset (Has_train,0,sizeof(Has_train)); thescanf"%d",&T); + for(inti =1; I < n; i + +){ -scanf"%d",&timecost[i]); $ } $scanf"%d", &l);//L Car - for(inti =0; I < L; i + +){ -scanf"%d", &ls[i]);//Departure Time thehas_train[ls[i]][1][0] =true; - }Wuyi for(inti =0; I < L; i + +){ the intTime =Ls[i]; - for(intj =2; J <= N; J + +){ WuTime + = timecost[j-1]; -has_train[time][j][0] =true; About } $ } -scanf"%d",&R); - for(inti =0; I < R; i + +){ -scanf"%d",&rs[i]); Ahas_train[rs[i]][n][1] =true; + } the for(inti =0; I < R; i + +){ - intTime =Rs[i]; $ for(intj = N1; J >=1; J--){ theTime + =Timecost[j]; thehas_train[time][j][1] =true; the } the } - in for(inti =1; I <= N-1; i + +) dp[t][i] =INF; theDp[t][n] =0; the } About voidsolve () { the for(inti = t1; I >=0; I--){ the for(intj =1; J <= N; J + +){ theDP[I][J] = dp[i+1][J] +1; + if(J < n && has_train[i][j][0] && I+timecost[j] <=T) -Dp[i][j] = min (dp[i][j],dp[i+timecost[j]][j+1]); the if(J >1&& has_train[i][j][1] && i+timecost[j-1] <=T)BayiDp[i][j] = min (dp[i][j],dp[i+timecost[j-1]][j-1]); the the } - } -printf"Case Number %d:", kase++); the if(dp[0][1] >= INF) cout <<"Impossible"<<Endl; the Elseprintf"%d\n", dp[0][1]); the the } - the intMain () { the while(SCANF ("%d", &n)! = EOF &&N) { the Input_pre ();94 solve (); the } the return 0; the}
UVA 1025 A Spy in the Metro (DAG dp)