CCF 201409-4 Optimal Food pairing

Source: Internet
Author: User

Question number: 201409-4
Question Name: Optimal food Pairing
Time limit: 1.0s
Memory Limit: 256.0MB
Description: Problem Description 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 store 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). The lines in the
Grid chart represent the roads 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 delivering meals is reflected in the time spent on the road, and it costs 1 yuan for each meal to walk one unit. 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, each line of two integers, Xi, Yi, represents the horizontal and vertical coordinates of a branch of a building in a square chart.
Next K line, three integers per line 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 row, two integers per line, representing 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 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 use case size and the first 30% of the evaluation case meet: 1<=n <=20. The
Top 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.

See the topic, first of all, from the store to the customer to the BFS, the hand-written when suddenly thought that if from the store to the customer, that every store to each customer to calculate the distance, and from customers to store only with BFS to a store end can, at that time also secretly stole happy, didn't think into another pit.

First on the customer to the store code, only 60 points

#include <iostream> #include <vector> #include <queue>using namespace Std;typedef struct{int x;// Customer coordinates x int y;//customer coordinates y int deep;//distance from the nearest branch of the current coordinates}ke;//the customer structure int status[1005][1005]={0};//The status of each point, is the branch 1, or the customer 2, or not the point 3 Default 0int map[1005][1005]={0};//If it is a customer, store the customer demand on each point int stamp[1005][1005]={0};//0 means no traversal, 1 means that the queue is going to traverse 2 for a full traversal Vector<ke > vec;//storage for all customers, easy to calculate int n,m,k,d;//Note, cannot be greater than n less than 1 and status cannot be 3 boundary value//here the nearest branch from customer BFS is more convenient int BFS (int x,int y) {int i,j;//Reset traversal State for (i=0;i<=n;i++) {for (j=0;j<=n;j++) {stamp[i][j]=0;}} Use 3 to surround the map, judging the line can not get through when only the judge status is not 3, main has been implemented queue<ke> que; Ke Tmp1;tmp1.x=x;tmp1.y=y;tmp1.deep=0;que.push (TMP1); Stamp[x][y]=1;while (Que.size () >0) {ke tmp = que.front ();// cout<<tmp.x<< "" <<tmp.y<< "" <<tmp.deep<<endl;que.pop ();//Left if (Status[tmp.x-1] [Tmp.y]==1] return tmp.deep+1;if (stamp[tmp.x-1][tmp.y]==0&&status[tmp.x-1][tmp.y]!=3) {stamp[tmp.x-1][ Tmp.y]=1; Ke Tmpxy; tmpxy.x=tmp.x-1;tmpxy.y=tmp.y;tmpxY.deep = Tmp.deep+1;que.push (TMPXY);} Right if (status[tmp.x+1][tmp.y]==1) return tmp.deep+1;if (stamp[tmp.x+1][tmp.y]==0&&status[tmp.x+1][tmp.y]!= 3) {stamp[tmp.x+1][tmp.y]=1; Ke tmpxy; tmpxy.x=tmp.x+1;tmpxy.y=tmp.y;tmpxy.deep = Tmp.deep+1;que.push (TMPXY);} On if (status[tmp.x][tmp.y-1]==1) return tmp.deep+1;if (stamp[tmp.x][tmp.y-1]==0&&status[tmp.x][tmp.y-1]!= 3) {stamp[tmp.x][tmp.y-1]=1; Ke tmpxy; tmpxy.x=tmp.x;tmpxy.y=tmp.y-1;tmpxy.deep = Tmp.deep+1;que.push (TMPXY);} Next if (status[tmp.x][tmp.y+1]==1) return tmp.deep+1;if (stamp[tmp.x][tmp.y+1]==0&&status[tmp.x][tmp.y+1]!= 3) {stamp[tmp.x][tmp.y+1]=1; Ke tmpxy; tmpxy.x=tmp.x;tmpxy.y=tmp.y+1;tmpxy.deep = Tmp.deep+1;que.push (TMPXY);} stamp[tmp.x][tmp.y]=2;}} int main () {Cin>>n>>m>>k>>d;int x,y,t;//uses 3 to surround the map, judging if the line is not working, only use the judge status is 3 for (int i=0;i <=n+1;i++) {status[0][i]=3;status[n+1][i]=3; status[i][0]=3;status[i][n+1]=3;} Enter branch for (int i=0;i<m;i++) {Cin>>x>>y;status[x][y] = 1;} Enter Customer Ke tmp;for (int i=0;i<k;i++) {Cin>>x>>y>>t;status[x][y] = 2;//If the demand for this point is 0, the new customer is added, otherwise the customer is not added if (Map[x][y] ==0) {tmp.x=x;tmp.y=y; Vec.push_back (TMP);} map[x][y]+=t;} for (int i=0;i<d;i++) {Cin>>x>>y;status[x][y] = 3;} Long long result=0;//for (int i=0;i<n+3;i++) {for (int j=0;j<n+3;j++) cout<<status[i][j]<< ""; cout <<endl;} for (int i=0;i<vec.size (); i++) {tmp = Vec[i];long long len = BFS (tmp.x,tmp.y);//cout<<tmp.x<<tmp.y< <len<< "\t\txxx" <<endl;; RESULT+=LEN*MAP[TMP.X][TMP.Y];} Cout<<result<<endl;return 0;}

After the submission timed out only 60 points, can not think of a simpler way, the online look at the other people's ideas, only found from the store to the customer traverse. So I wrote it again from the store to the customer (in order to start with each store coordinates for the whole picture BFS), and finally just scored 90 points. It was later discovered that there was a coincidence in this traversal, and that it was a good idea to add the branch to the initial queue of the BFS at the same time.

Code:

#include <iostream> #include <vector> #include <queue>using namespace std;typedef struct Node{int x;// Customer coordinates x int y;//customer coordinates y int deep;//distance from the nearest branch of the current coordinate node (int a=0,int b=0,int c=0) {x=a;y=b;deep=c;}} KE;//customer structure, can also be used to save the branch coordinates at this time deep meaningless short status[1005][1005]={0};//the status of each point, is the branch 1, or Customer 2, or can not go through the point 3 default 0int map[1005][1005 ]={0};//If you are a customer, store customer demand on each point short stamp[1005][1005]={0};//0 means no traversal, 1 means that the queue is going to traverse 2 to fully traverse int minlen[1005][1005];// Represents the distance from the nearest store for each point, which is initialized with the maximum int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};vector<ke> vec;//store all customers, facilitating the calculation of total cost vectors behind <Ke> dian;//Store all stores, as the beginning of the traversal int n,m,k,d;//Note, cannot be greater than n less than 1 and status cannot be 3 boundary value void BFS () {int I,j;int kecount = 0;//Reset Traverse State for (i=0;i<=n;i++) {for (j=0;j<=n;j++) {stamp[i][j]=0;}} Use 3 to surround the map, judging the line can not be used only when the judge status is 3 for (int i=0;i<=n+1;i++) {status[0][i]=3;status[n+1][i]=3; status[i][0]=3 ; status[i][n+1]=3;} Queue<ke> que;for (I=0;i<dian.size (); i++) {Que.push (dian[i]);} while (Que.size () >0&&kecount<=k) {Ke tmp = QUE.FRont ();//cout<<tmp.x<< "" <<tmp.y<< "" <<tmp.deep<<endl;que.pop (); int deep = Tmp.deep;for (i=0;i<4;i++) {int x = Tmp.x+dir[i][0];int y = tmp.y+dir[i][1];if (Stamp[x][y]==0&&status[x][y] !=3) {Stamp[x][y] = 1;minlen[x][y] = Deep+1;que.push (Ke (x,y,deep+1)); if (status[x][y]==2) kecount++;}} stamp[tmp.x][tmp.y]=2;}} int main () {Cin>>n>>m>>k>>d;int x,y,t; Ke tmp;//initializes the distance from the nearest point for each point to the maximum value for (int i=0;i<=n+1;i++) {for (int j=0;j<=n+1;j++) minlen[i][j] = 1000000;} Enter branch for (int i=0;i<m;i++) {Cin>>x>>y;status[x][y] = 1;tmp.x=x;tmp.y=y; Dian.push_back (TMP);} Enter customer for (int i=0;i<k;i++) {Cin>>x>>y>>t;status[x][y] = 2;//If the demand for this point is 0, the new customer is added, otherwise the customer is not added if (map[x][ y]==0) {tmp.x=x;tmp.y=y; Vec.push_back (TMP);} map[x][y]+=t;} for (int i=0;i<d;i++) {Cin>>x>>y;status[x][y] = 3;} Long Long result=0; BFS (); for (int i=0;i<vec.size (); i++) {tmp = VEC[I];RESULT+=MINLEN[TMP.X][TMP.Y]*MAP[TMP.X][TMP.Y];} Cout<<resUlt<<endl;return 0;} 

  

CCF 201409-4 Optimal Food pairing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.