Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.7 kB
1
Indexable
Never
/*Our friend Picko is very reach and he wants to open lots of restaurants with delivery. The main food will be, of course, pizza. He has certain number of potential locations for the restaurants, and he knows the locations of the solitaires with lots of people which will often be his customers. Delivery of each restaurant will cover all the solitaires in given radius.

Picko can open only limited number of restaurants, and he wants that restaurants on the locations which will cover maximal number of people in solitaires.

Write a program that will calculate maximal number of people which we can cover with delivery.

 

Input

In the first line of the input file there are two integers K and R, separated with space, number of restaurants and radius of delivery, 1 ≤ K ≤ 10, 1 ≤ R ≤ 500.

In the second line there is integer M, number of locations, K ≤ M ≤ 20.

In each of the next M lines there are two integer X and Y, separated with space, coordinates of each location, -1000 ≤ X,Y ≤ 1000.

In the next line there is integer N, number of solitaires, 1 ≤ N ≤ 100.

In each of the next N lines there are three integers X, Y and S, separated with space, X and Y are coordinates of each solitaire, and S is number of people in that solitaire, -1000 ≤ X,Y ≤ 1000, 1 ≤ S ≤ 100.

We consider that solitaire is in radius of some restaurant if distance between them is less or equal to R. There are no two locations of restaurants on the same place.

 

Output

In only line of the output file we have to write maximal number from the text above.

 

Sample



Input

3

2 2

3

1 0

4 0

7 0

4

0 0 1

3 0 7

5 0 9

8 0 1

2 2

3

-2 0

0 1

3 0

8

-3 1 1

-3 0 1

-3 -1 1

-2 -1 1

0 0 3

0 2 1

2 1 3

4 0 2

3 3

5

0 0

1 6

2 3

6 6

7 2

8

0 1 2

0 5 3

0 6 1

1 0 1

3 2 3

3 6 2

6 2 4

8 6 3



Output

#1 18

#2 12

#3 17 */
#include <iostream>
using namespace std;

#define MAX 100000
class Queue
{
	int front;
	int near;
	int a[MAX];
public:	
	Queue();
	bool EnQueue(int x);
	bool isEmpty();
	bool fullQueue();
	int DeQueue();
	int peek();
	void reset();
};
Queue::Queue()
{
	front = -1;
	near = -1;
}
bool Queue::fullQueue()
{
	return (MAX-1 == this->near);
}
bool Queue::isEmpty()
{
	return (this->near == this->front);
}
bool Queue::EnQueue(int x)
{
	if(this->fullQueue())
		return false;
	this->a[++this->near] = x;
	return true;
}
int Queue::DeQueue()
{
	if(this->isEmpty())
		return -1;
	return this->a[++this->front];
}
int Queue::peek()
{
	if(this->isEmpty())
		return -1;
	return this->a[this->front+1];
}
void Queue::reset()
{
	front = -1;
	near = -1;
}
struct Node
{
	int x,y,diem;
};

Node nodeDC[100];
Node nodeNH[20];
Queue queueX;
Queue queueY;
int K,R,M,N;
int arr[1000][1000];
int arrKhoangCach[100][100];
int visitNH[20];
int visitDC[100];
int Max;
int Sum;

bool kc(int i, int j)
{
	int k = (nodeNH[i].x - nodeDC[j].x)*(nodeNH[i].x - nodeDC[j].x) + (nodeNH[i].y - nodeDC[j].y)*(nodeNH[i].y - nodeDC[j].y);
	return (k <= R*R);
}

void removeCus(int i)
{
	for(int j = 0 ; j < N ; j++)
	{
		if(visitDC[j] == i+1)
		{
			Sum -= nodeDC[j].diem;
			visitDC[j] = 0;
		}
	}
}

bool addCus(int i)
{
	bool change = false;
	for(int j = 0 ; j < N ; j++)
	{
		if(visitDC[j] == 0)
		{
			if(kc(i,j))
			{
				change = true;
				Sum += nodeDC[j].diem;
				visitDC[j] = i+1;
			}
		}
	}
	return change;
}
bool haveCus()
{
	bool check = false;
	for(int i = 0 ; i < N ; i++)
		if(visitDC[i]==0)
		{
			check = true;
			break;
		}
	return check;
}
void solve(int index)
{
	if(index == K || !haveCus())
	{
		if(Sum > Max) Max = Sum;
		return;
	}
	for(int i = 0 ; i < M ; i++)
	{
		if(visitNH[i] == 0)
		{
			visitNH[i] = 1;
			if(addCus(i)){
			solve(index+1);
			removeCus(i);
			}
			visitNH[i] = 0;
		}		
	}
}
void resetvisit()
{
	for(int i = 0 ; i < 100 ; i++)
	{
		visitDC[i] = 0;
	}
	for(int i = 0 ; i < 20 ; i++)
	{
		visitNH[i] = 0;
	}
}
int main()
{
//	freopen("input.txt" , "r" , stdin);
	int T;
	cin >> T;
	for(int testCase = 1; testCase <= T ; testCase++)
	{
		//reset visit
		resetvisit();
		cin >> K >> R;
		cin >> M;
		for(int i = 0 ; i < M ; i++)
		{
			cin >> nodeNH[i].x >> nodeNH[i].y;	
		}
		cin >> N;
		for(int i = 0 ; i < N ; i++)
		{
			cin >> nodeDC[i].x >> nodeDC[i].y >> nodeDC[i].diem;
		}
		Sum = 0;
		Max = -1;
		solve(0);
		cout << "#" << testCase << " "<< Max << endl;
	}
	return 0;
}