topics from the September 2014 CCF Computer professional qualification exam
Problem DescriptionThe building has recently opened a restaurant chain offering takeaway services. With more and more chain stores, how reasonable to give customers a meal has become an urgent problem to solve.
The area of the building's chain can be seen as a nxn (as shown), the location of the lattice on the grid may include the branch (green label) or the customer (blue callout), and some of the lattice points cannot be passed (red callout).
A line in a grid chart represents a road that can be walked, with a distance of 1 adjacent to the two lattice points. The building must take the road that can be walked, and cannot be marked by red dots.
The main cost of delivery is reflected in the time spent on the road, each meal to walk a unit of distance will cost 1 yuan. Each customer's needs can be delivered from any branch of the building, with no limit on the total distribution of each branch.
Now that you've got the needs of the building's customers, how much will it cost to send these meals under the best delivery mode?
Input FormatThe first line of input contains four integers n, m, K, D, respectively, indicating the size of the square chart, the number of branches in the building, the number of customers, and the number of points that cannot be passed.
Next M-line, two integers per line xi, Yi, represents the horizontal and vertical coordinates of a branch of a building in a square chart.
The next K line, each line of three integer xi, Yi, CI, respectively, each customer in the grid chart of the horizontal axis, ordinate and order the amount of food. (Note that there may be multiple customers in the same position in the grid chart)
The next D line, two integers per line, represents the horizontal and vertical coordinates of each point that cannot be passed.
output FormatOutputs an integer that represents the cost to be spent in the optimal feeding mode.
Sample Input10 2 3 3
1 1
8 8
1 5 1
2 3 3
6 7 2
1 2
2 2
6 8
Sample Output29
Measuring use case size and conventionsThe first 30% evaluation cases meet: 1<=n <=20.
The first 60% evaluation cases meet: 1<=n<=100.
All evaluation cases are met: 1<=n<=1000,1<=m, K, d<=n^2. There may be multiple customers on the same grid point. Each customer's order quantity does not exceed 1000, each customer needs the meal to be able to be sent.
Problem Analysistitle is a variant of breadth-first searchthe position of the initial queue is not a point but multiple points, that is, from the location of multiple stores and start breadth First searchyou can save the distance from each point in the grid to the nearest store in the matrix, and then traverse the customer's costingThe result will exceed the representation range of int
#include"iostream"#include"Vector"#include"Queue"using namespacestd;intdist[1001][1001];BOOLvisited[1001][1001];intwidth, numstore, numclient, Numhole;structPos {intx, Y, Val; Pos (int_x,int_y): X (_x), Y (_y) {} Pos () {}};BOOLLegalintXinty) {returnx>=1&& y>=1&& x<=width && y<=width;} Pos dir[4] = {{0,1}, {0, -1}, {1,0}, {-1,0}};intMain () {CIN>> width >> numstore >> numclient >>Numhole; Queue<Pos>stores; Vector<Pos>holes, clients; Pos p; for(intI=0; i<numstore; i++) {cin>> p.x >>p.y; Stores.push (P); DIST[P.X][P.Y]=0; VISITED[P.X][P.Y]=true; } for(intI=0; i<numclient; i++) {cin>> p.x >> p.y >>P.val; Clients.push_back (P); } for(intI=0; i<numhole; i++) {cin>> p.x >>p.y; DIST[P.X][P.Y]= -1; VISITED[P.X][P.Y]=true; } while(!Stores.empty ()) {Pos P=Stores.front (); Stores.pop (); for(intI=0; i<4; i++) {Pos np (p.x+dir[i].x, p.y+dir[i].y); if(!visited[np.x][np.y] &&Legal (np.x, NP.Y)) {VISITED[NP.X][NP.Y]=true; DIST[NP.X][NP.Y]= Dist[p.x][p.y] +1; Stores.push (NP); } } } Long LongCost =0; for(intI=0; i<numclient; i++) { cost+ = dist[clients[i].x][clients[i].y] *Clients[i].val; } cout<<Cost ;}
CCF 201409-4 Optimal Food pairing