Optimal catering and nutritional catering
Problem description Dong recently opened a restaurant chain to provide take-out services. With the increasing number of chain stores, how to reasonably deliver meals to customers has become an urgent problem.
The region where the chain stores are located can be regarded as a square map of n × n (as shown in). The positions on the square points may contain the branches of the Building (marked in green) or the customer (marked in blue) has some points that cannot pass through (marked in red ).
The line in the square chart indicates the road that can be walked. The distance between the two neighboring points is 1. To deliver meals, the building must follow a road that can be walked and cannot be marked in red.
The main cost of food delivery is the time spent on the road. Each meal takes 1 RMB for every unit. Each customer's needs can be distributed by any branch of the building. Each branch has no limit on the total delivery volume.
Now you have the requirements of Dongdong customers. How much does it cost to deliver these meals in the optimal way. The first line in the input format contains four integers: n, m, k, and d, indicating the size of the square chart, the number of branches, and the number of customers, and the number of points that cannot pass through.
In the next m row, each line contains two integers xi and yi, indicating the horizontal and vertical coordinates of a branch in the building.
In the next k rows, each line contains three integers xi, yi, and ci, which indicate the abscissa, ordinate, and order quantity of each customer in the square chart. (Note that there may be multiple customers in the same position in the square chart)
Next line d, two integers in each line, respectively, represent the horizontal and vertical coordinates of each point that cannot pass through. The output format outputs an integer to indicate the cost of optimal meal delivery. Example input 10 2 3 3
1 1
8
1 5 1
2 3 3
6 7 2
1 2
2 2
6 8 Examples output 29 the scale of the evaluation case meets the requirements of the First 30% of the evaluation cases agreed upon: 1 <= n <= 20.
The first 60% of evaluation cases meet the following requirements: 1 <= n <= 100.
All evaluation cases meet the following requirements: 1 <= n <= 1000,1 <= m, k, d <= n ^ 2. Multiple customers may be on the same grid point. Each customer's order quantity cannot exceed 1000, and each customer's required meals can be delivered.
Bfs.
# Include <stdlib. h> # include <stdio. h> # include <string. h >#include <iostream >#include <queue> using namespace std; struct State {int x, y; int layer; State (int _ x, int _ y, int _ l = 0): x (_ x), y (_ y), layer (_ l) {} State () {x = y = layer = 0 ;}}; int data [1000 + 5] [1000 + 5] = {0 }; // 0 0/1 vis 1 0/1 client other clients needs; std: queue <State> que; int n, m, k, d, x, y, z; long ans; void bfs (); int main () {scanf ("% d", & n, & m, & k, & d ); For (int I = 0; I <m; I ++) // branch {scanf ("% d", & x, & y); que. push (State (x, y) ;}for (int I = 0; I <k; I ++) {scanf ("% d ", & x, & y, & z); data [x] [y] = (data [x] [y] | 2) + (z <2 );} for (int I = 0; I <d; I ++) {scanf ("% d", & x, & y ); data [x] [y] | = 1;} bfs (); return 0;} void bfs () {ans = 0; State tmp, cur; while (! Que. empty () {cur = que. front (); que. pop (); int curx = cur. x, cury = cur. y, curl = cur. layer; if (curx> 0 & cury> 0 & curx <= n & cury <= n &&! (Data [curx] [cury] & 1) {data [curx] [cury] | = 1; que. push (State (curx + 1, cury, curl + 1); que. push (State (curx, cury + 1, curl + 1); que. push (State (curx-1, cury, curl + 1); que. push (State (curx, cury-1, curl + 1); if (data [curx] [cury] & 2) ans + = (data [curx] [cury]> 2) * curl ;}} cout <ans; // printf ("% d", ans );}