The first time, Liu rujia prompts and solves the problem. Let's look back !!!
Point:
DP [time] [sta]; the minimum waiting time for the sta at the station at the time;
The status of the end point is definitely "DP [T] [N] = 0"-that is, the end point exactly reaches N sites at the T moment.
We can switch from the end state to the starting state, step by step.
Has_train [T] [I] [0]; t indicates whether there is a train to the right at station I.
Has_train [T] [I] [1]; t indicates whether there is a left-facing train at station I.
# Include <iostream>
# Include <sstream>
# Include <cstdio>
# Include <cstring>
# Include <cmath>
# Include <string>
# Include <vector>
# Include <set>
# Include <cctype>
# Include <algorithm>
# Include <cmath>
# Include <deque>
# Include <queue>
# Include <map>
# Include <stack>
# Include <list>
# Include <iomanip>
Using namespace STD;
# Define INF 0xffffff7
# Define maxn 200 + 10
Int N, T;
Int T [maxn];
Int DP [maxn] [maxn]; // DP [I] [J] I: The minimum waiting time at station J.
Int has_train [maxn] [maxn] [2];
Int main ()
{
// Freopen ("out.txt", "r", stdout );
Int Kase = 0;
Int N;
While (scanf ("% d", & N)
{
Memset (has_train, 0, sizeof (has_train ));
Memset (T, 0, sizeof (t ));
Kase ++;
Scanf ("% d", & T );
For (INT I = 1; I <n; I ++)
Scanf ("% d", & T [I]);
Int M1, M2;
Scanf ("% d", & M1 );
For (INT I = 1; I <= m1; I ++)
{
Int start;
Scanf ("% d", & START );
Has_train [start] [1] [0] = 1; // mark the initial platform
For (Int J = 1; j <n; j ++)
{
Int time = start + T [J];
If (time <= T)
{
Has_train [time] [J + 1] [0] = 1;
Start + = T [J];
}
}
}
Scanf ("% d", & m2 );
For (INT I = 1; I <= m2; I ++)
{
Int start;
Scanf ("% d", & START );
Has_train [start] [N] [1] = 1; // mark the initial platform
For (Int J = N-1; j> = 1; j --)
{
Int time = start + T [J];
If (time <= T)
{
Has_train [time] [J] [1] = 1;
// Cout <"has [" <time <"] [" <j <"] [1]" <Endl;
Start + = T [J];
}
}
}
Printf ("Case Number % d:", Kase );
For (INT I = 1; I <n; I ++)
DP [T] [I] = inf;
DP [T] [N] = 0;
For (INT I = T-1; I> = 0; I --)
{
For (Int J = 1; j <= N; j ++)
{
DP [I] [J] = DP [I + 1] [J] + 1; // The minimum waiting time for the T-1 moment at station J = the minimum waiting time for the T moment at station J + 1; that is, decision 1 --- wait for one minute
If (j <n & has_train [I] [J] [0] & I + T [J] <= T) // Decision 2
{
DP [I] [J] = min (DP [I] [J], DP [I + T [J] [J + 1]);
}
If (j> 1 & has_train [I] [J] [1] & I + T [J-1] <= T) // Decision 3
{
DP [I] [J] = min (DP [I] [J], DP [I + T [J-1] [J-1]);
}
}
}
If (DP [0] [1]> = inf) printf ("impossible \ n ");
Else printf ("% d \ n", DP [0] [1]);
}
Return 0;
}