Labels: Linear Planning and network flow 24-question car fuel driving problem hierarchy chart
Time Limit: 1 sec memory limit: 128 MB
Description
Given a square grid of N * n, set the upper left corner of the grid as the starting point ◎, the coordinate is (), the X axis to the right is positive, the Y axis to the downward is positive, and the side length of each square is 1 ,. A car departs from the starting point ◎ and goes to the bottom right corner of the terminal. The coordinate is (N, N ). An oil depot is set up at several grid intersections for a vehicle to refuel during driving. The following rules should be observed during Automobile Driving:
(1) A car can only drive along the grid side. After full of oil, it can drive K grid sides. The car is full of oil at departure and there is no oil depot at the start and end points.
(2) If the X or Y coordinates of an automobile go through a grid edge are reduced, B is charged. Otherwise, no fee is charged.
(3) When a car is driving, it shall be filled with oil and fuel a shall be paid.
(4) If necessary, you can add an oil depot at the grid point, and pay the additional oil depot fee C (excluding fuel ).
(5) (1 )~ (4) The numbers N, K, A, B, and C are positive integers, and the constraint is 2 ≤ n ≤, 2 ≤ k ≤ 10. Design an algorithm to find the vehicle's route from the start point to the end point.
For a given traffic grid, calculate a vehicle's travel route from the start point to the end point with the least cost.
Input
The first line of the file is the value of N, K, A, B, and C. Starting from the second row is a 0-1 matrix of N * n, with N values in each row ending with n + 1. The value in column J of row I of the phalanx is 1, indicating that an oil depot is set at the grid intersection (I, j). If it is 0, it indicates that no oil depot is set. Numbers adjacent to each row are separated by spaces.
Output
Minimum output cost
Sample input9 3 2 3 60 0 0 0 1 0 0 00 0 0 1 0 1 1 1 0 01 0 0 0 0 1 00 0 0 0 0 1 0 0 0 0 11 0 0 1 0 0 1 0 00 1 0 0 0 0 0 1 00 0 0 1 0 0 0 0 11 0 0 1 0 0 1 00 1 0 0 0 0 0 0 0 0 sample output12source
Network stream 24 Questions
The question is still not described in detail, and it is not difficult to explain the problem. There are several transfer methods than the bare shortest path. I will not talk about the transfer equation, but the code is very beautiful.
1. There is a charge for B depending on the direction.
2. If the gas station is faulty, the fuel volume can be fully transferred. Note that you do not need to build the fuel.
# Include <queue> # include <cstdio> # include <cstring> # include <algorithm> # define N 105 # define K 15 # define INF 0x3f3f3fusing namespace STD; const int DX [4] = {,-}; const int dy [4] = {, 0,-1}; struct Lux {int K, X, Y; lux (int A, int B, int C): K (A), x (B), y (c) {} LUX (){}}; int map [N] [N], Id [N] [N], CNT; int N, P, A, B, C; int Dist [k] [N] [N]; bool in [k] [N] [N]; Lux S, T; int spfa () {int I, VX, vy, weight, fee2; queue <lux> q; memset (Dist, 0x3f, sizeof (DIST); Dist [S. k] [S. x] [S. y] = 0; in [S. k] [S. x] [S. y] = 1; q. push (s); While (! Q. empty () {Lux u = Q. front (); q. pop (); in [U. k] [U. x] [U. y] = 0; If (! U. k) continue; // For (products = I = 0; I <4; I ++) {vx = u. X + dx [I]; Vy = u. Y + dy [I]; if (I = 2) then = B;/* the extra charge for going back is handled here. */If (! Id [VX] [Vy]) continue; If (! Map [VX] [Vy]) {fee2 = C;/* newly opened petrol stations. To be precise, the idea here is to make all the diagrams open into gas stations. The original gas stations are forced to refuel, the new gas station is not mandatory, but pay more for fuel */If (U. K & Dist [U. k-1] [VX] [Vy]> Dist [U. k] [U. x] [U. y] + minutes) {/* because gas stations are forced to refuel, it is not necessary to transfer gas stations. */Dist [U. k-1] [VX] [Vy] = DIST [U. K] [U. x] [U. Y] + then; If (! In [U. k-1] [VX] [Vy]) in [U. k-1] [VX] [Vy] = 1, Q. push (LUX (U. k-1, VX, Vy) ;}} else fee2 = 0; /* you do not need to pay extra fees if you already have an existing gas station */If (Dist [p] [VX] [Vy]> Dist [U. k] [U. x] [U. y] + A + weight + fee2) {/* fuel transfer */Dist [p] [VX] [Vy] = DIST [U. k] [U. x] [U. y] + A + second + fee2; If (! In [p] [VX] [Vy]) in [p] [VX] [Vy] = 1, Q. push (LUX (p, VX, Vy) ;}} int ret = inf; for (I = 0; I <= P; I ++) ret = min (Ret, DIST [I] [T. x] [T. y]); return ret;} int main () {// freopen ("test. in "," r ", stdin); scanf (" % d ", & N, & P, & A, & B, & C); For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) scanf ("% d", & map [I] [J]), Id [I] [J] = ++ CNT; S = LUX (P, 1, 1 ), T = LUX (0, n, n); printf ("% d \ n", spfa (); Return 0 ;}
[24 questions about linear planning and network flow] stratified diagram of vehicle fuel driving problems