Car refueling problem time limit: 1 Sec Memory Limit: MB
Description
Given a n*n square grid, set its upper-left corner as the starting point, the coordinates are (n), the x-axis right is positive, the y-axis is positive, each square side length is 1, see. A car from the starting point to the lower end of the right corner ▲, the coordinates are (n,n). At the intersection of several grids, an oil depot is set up for the car to refuel on the way. For example, the following rules should be followed for the vehicle during driving:
(1) The car can only travel along the grid side, fill the oil after the K-grid edge. The car was filled with oil at the time of departure and there was no oil depot at the starting and ending points.
(2) When the car passes through a grid edge, if its x-coordinate or y-coordinate is reduced, the cost B is payable, otherwise the fee is waived.
(3) vehicles in the process of the oil depot should be filled with fuel and to pay the refueling cost A.
(4) in the case of the need to add oil depot at the grid point, and to pay the additional oil depot costs C (excluding refueling costs A).
(5) (1) ~ (4) The number of N, K, A, B, c are positive integers, and meet the constraints: 2≤n≤100,2≤k≤10. An algorithm is designed to find the minimum number of routes that a car pays to reach its destination from its starting point.
For a given traffic grid, the car is calculated from the point of departure to the end of a line paid with the fewest travel routes.
Input
The first line of the file is the value of n,k,a,b,c. The second line is a n*n 0-1 square, each row N values, to the end of the n+1 line. A value of 1 at column J of the square of the phalanx means that an oil depot is set at the intersection of the grid (I,J) and 0 indicates no oil depot. Each row is adjacent to two digits separated by a space.
Output
Minimum cost of output
Sample Input9 3 2 3 60 0 0 0 1 0 0 0 00 0 0 1 0 1 1 0 01 0 1 0 0 0 0 1 00 0 0 0 0 1 0 0 11 0 0 1 0 0 1 0 00 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 xx 1 0 0 0 0 0 0 0Sample Output ASource
Network Flow 24 Questions
Test instructions still do not add, the puzzle is not hard to say, is more than bare the shortest way more than a few transfer methods, I do not mention the transfer equation, the code is very beautiful writing.
1. There must be a cost B depending on the direction.
2. Gas station problem, that is able to transfer the amount of oil into full, pay attention to the need to build it.
#include <queue> #include <cstdio> #include <cstring> #include <algorithm> #define N 105#define K 15#define inf 0x3f3f3f3fusing namespace std;const int dx[4]={1,0,-1,0};const int dy[4]={0,1,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,fee,fee2;queue<lux>q;memset (dist,0x3f,sizeof (Dist));d Ist[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;//run out of gas what's going on? for (fee=i=0;i<4;i++) {vx=u.x+dx[i];vy=u.y+dy[i];if (i==2) fee=b;/* go back and pay more for the cost of handling it here. */if (!id[vx][vy]) continue;if (!map[vx][vy]) {fee2=c;/* New oil station, the idea is that the whole picture is opened to a gas station, the original gas station forced refueling, new gas station is not mandatory, but refueling to pay more */if (U.K &&dist[u.k-1][vx][vy]>dist[u.k][u.x][u.y]+fee) {/* Because petrol stations force refueling, so not the gas station ability to transfer. */dist[u.k-1][vx][vy]=dist[u.k][u.x][u.y]+fee;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;/* already have the original refuelingYou don't have to pay an extra fee to stand up. */if (dist[p][vx][vy]>dist[u.k][u.x][u.y]+a+fee+fee2) {/* Refueling transfer */dist[p][vx][vy]=dist[u.k][u.x][ U.y]+a+fee+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%d%d%d%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);p rintf (" %d\n ", SPFA ()); return 0;}
"24 Questions on linear programming and network flow" a hierarchical map of automobile refueling problems