裸二分图匹配
/*--------------------------------------------------------------------------------------*/ #include <algorithm>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map> //debug function for a N*M array
#define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\
{for(int j=;j<(M);j++){\
printf("%d",G[i][j]);}printf("\n");}
//debug function for int,float,double,etc.
#define debug_var(X) cout<<#X"="<<X<<endl;
#define LL long long
const int INF = 0x3f3f3f3f;
const LL LLINF = 0x3f3f3f3f3f3f3f3f;
/*--------------------------------------------------------------------------------------*/
using namespace std; int N,M,T,t;
const int maxn = ;
vector <int> G[maxn];
int uN;
int Mx[maxn],My[maxn];
int dx[maxn],dy[maxn];
int dis;
bool used[maxn];
bool SearchP()
{
queue<int> Q;
dis = INF;
memset(dx,-,sizeof dx);
memset(dy,-,sizeof dy);
for(int i=;i<=uN;i++)
{
if(Mx[i] == -)
{
Q.push(i);
dx[i] = ;
}
}
while(!Q.empty())
{
int u = Q.front();
Q.pop();
if(dx[u] > dis) break;
int sz = G[u].size();
for(int i=;i<sz;i++)
{
int v = G[u][i];
if(dy[v] == -)
{
dy[v] = dx[u] + ;
if(My[v] == -) dis = dy[v];
else
{
dx[My[v]] = dy[v] + ;
Q.push(My[v]);
}
}
}
}
return dis != INF;
}
bool DFS(int u)
{
int sz = G[u].size();
for(int i=;i<sz;i++)
{
int v = G[u][i];
if(!used[v] && dy[v] == dx[u]+)
{
used[v] = true;
if(My[v] != - && dy[v] == dis) continue;
if(My[v] == - || DFS(My[v]))
{
My[v] = u;
Mx[u] = v;
return true;
}
}
}
return false;
} int MaxMatch()
{
int res = ;
memset(Mx,-,sizeof Mx);
memset(My,-,sizeof My);
while(SearchP())
{
memset(used,false,sizeof used);
for(int i=;i<=uN;i++) if(Mx[i] == - && DFS(i))
res++;
}
return res/;
} typedef pair<int,int> point;
vector <point> gst;
int v[maxn]; int main()
{
scanf("%d",&T);
int cas = ;
while(T--)
{
scanf("%d%d",&t,&N);
for(int i=;i<maxn;i++) G[i].clear(); gst.clear();
for(int i=,x,y,s;i<=N;i++)
{
scanf("%d%d%d",&x,&y,&s);
gst.push_back(make_pair(x,y));
v[i] = s;
}
scanf("%d",&M);
uN = N+M;
for(int i=,x,y;i<=M;i++)
{
scanf("%d%d",&x,&y);
for(int g=;g<gst.size();g++)
{
if((x-gst[g].first)*(x-gst[g].first)+(y-gst[g].second)*(y-gst[g].second) <= t*v[g+]*t*v[g+])
{
G[g+].push_back(i+N);
G[i+N].push_back(g+);
//printf("link:[%d,%d]\n",g+1,i+N);
}
}
}
printf("Scenario #%d:\n%d\n\n",++cas,MaxMatch());
}
}