Uva_10201
We may use F [I] [J] to indicate the minimum cost required for this state when the fuel volume at the I gas station is J, so first of all, some of the states are inherited from the first gas station in I-1, that is, F [I] [J] = f [I-1] [J + d [I]-d [I-1], after that, it would be more cost-effective to consider how much oil to buy at the gas station I, then the state transition equation is f [I] [J] = min {f [I] [k] + (J-k) * m [I]}, f [I] [J]}, which seems to be three for, but for min {f [I] [k] + (J-K) * m [I]} can also be written as f [I] [k]-K * m [I] + J * m [I], f [I] [k]-K * m [I] is only related to K, we can record the minimum f [I] [J]-J * m [I] in the loop J. Set it to temp, then the second dynamic transfer equation becomes f [I] [J] = min {temp + J * m [I]}, F [I] [J]}.
# Include <stdio. h>
# Include < String . H>
# Define Maxd 110
# Define # Maxl 210
# Define INF 1000000000
Int L, N, F [maxd] [maxl], d [maxd], M [maxd];
Char B [ 100 ];
Void Init ()
{
Gets (B );
Sscanf (B, " % D " , & L );
N = 0 ;
D [ 0 ] = 0 ;
While (Gets (B )! = NULL)
{
If (B [ 0 ] = ' \ 0 ' )
Break ;
++ N;
Sscanf (B, " % D " , & D [N], & M [N]);
If (D [N] <0 | D [N]> L)
-- N;
}
}
Void Solve ()
{
Int I, J, K, temp, DIS;
For (I = 0 ; I <= N; I ++)
For (J = 0 ; J <= 200 ; J ++)
F [I] [J] = inf;
F [0 ] [ 100 ] = 0 ;
For (I = 1 ; I <= N; I ++)
{
Dis = d [I]-d [I- 1 ];
For (J = 0 ; J + dis <= 200 ; J ++)
If (F [I- 1 ] [J + dis] <F [I] [J])
F [I] [J] = f [I- 1 ] [J + dis];
Temp = inf;
For (J = 0 ; J <= 200 ; J ++)
{
If (F [I] [J]-M [I] * j <temp)
Temp = f [I] [J]-M [I] * J;
If (Temp + M [I] * j <F [I] [J])
F [I] [J] = temp + M [I] * J;
}
}
If (L-D [N]> 100 | F [N] [ 100 + L-d [N] = inf)
Printf ( " Impossible \ n " );
Else
Printf ( " % D \ n " , F [N] [ 100 + L-d [N]);
}
Int Main ()
{
Int T;
Gets (B );
Sscanf (B, " % D " , & T );
Gets (B );
While (T --)
{
Init ();
Solve ();
If (T)
Printf ( " \ N " );
}
Return 0 ;
}