The 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? The first line entered in the input format contains four integers n, m, K, D, respectively, indicating the size of the 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. The output format outputs an integer that represents the cost to be spent under the optimal delivery mode. Sample Input 10 2 3 3
1 1
8 8
1 5 1
2 3 3
6 7 2
1 2
2 2
6 8 Sample Output 29 evaluation of the use case size and the agreement before 30% of the evaluation case to 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. PS: The following code, the bit operation is too esoteric, I also did not understand what • 1 meaning, but see the online god code, and then temporarily affixed to the code, and so on after the degree calculation to solve the problem, but I feel that the naked BFS can also be, summer training did a similar simulation + BFS labeling problem, is the red Luro of the BFS solution, but a little trouble, I do not like to knock on the following attached to the code of the Great God, for reference only
#include <cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<iostream>#include<queue>using namespacestd;intn,m,k,d; Shortmap[1005][1005],dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};BOOLWithin (intXinty) { if(x<=0|| x>n| | y<=0|| Y>N)return false; return true;}structnode{intX,y,step; Node (intA=0,intb=0,intC=0) {x=A; Y=b; Step=C; }};queue<node>Q;Long LongBFS () {node n; intt=0; Long Longsum=0; while(!Q.empty ()) {N=Q.front (); Q.pop (); intI,x,y; for(i=0;i<4; i++) {x=n.x+dir[i][0]; Y=n.y+dir[i][1]; if(Within (x, y) &&! (map[x][y]&1) ) {Map[x][y]|=1; if(map[x][y]&2) {sum+ = (map[x][y]>>2) * (n.step+1); T++; if(t==k)returnsum; } Q.push (Node (x,y,n.step+1)); } } }}intMain () { while(SCANF ("%d %d%d%d", &n,&m,&k,&d)! =EOF) { intI,j,x,y; memset (Map,0,sizeof(map)); for(i=0; i<m;i++) {scanf ("%d%d",&x,&y); Map[x][y]|=1; Q.push (node (x, Y,0)); } for(i=0; i<k;i++) {scanf (" %d%d%d",&x,&y,&j); Map[x][y]= (map[x][y]|2) + (j<<2); } for(i=0; i<d;i++) {scanf ("%d%d",&x,&y); Map[x][y]|=1; } printf ("%i64d\n", BFS ()); } return 0;}
CCF Authentication Problem Search question