Description
Input
Output
Sample Input
2 1 2 1 0 3 3 0 3 2 4 0 6 0 1 2 1 1 2 3 3 2 2 2 2 4 4
Sample Output
Scenario #1: 2 Scenario #2: 2
思路:Km超时,需要多路增广的Km
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<queue> #define FOR(i,a,b) for(int i=a;i<=b;++i) #define clr(f,z) memset(f,z,sizeof(f)) using namespace std; const int mm=3009; class Edge { public:int v,next; }e[mm*mm*2]; class _point { public:int x,y,dis; int fabs(int x) { if(x<0)return -x; return x; } void out() { printf("qw=%d %d %d \n",x,y,dis); } bool ok(_point t) { int d=fabs(x-t.x)+fabs(y-t.y); if(d<=dis) return 1; return 0; } }p[mm]; int head[mm],edge=0; int X[mm],Y[mm];bool T[mm]; void init() { clr(head,-1);edge=0; } void add(int u,int v) { e[edge].v=v;e[edge].next=head[u];head[u]=edge++; } //queue<int>Q; int Q[mm],front,rear; int n,m; int dx[mm],dy[mm]; bool bfs()///多路增广 { front=rear=0; FOR(i,1,n) if(Y[i]==-1) { Q[rear++]=i; //Q.push(i); } FOR(i,0,n)dx[i]=0; FOR(i,0,m)dy[i]=0; bool flag=0; int u,v; while(front<rear) { u=Q[front++];//Q.front();Q.pop(); for(int i=head[u];~i;i=e[i].next) { v=e[i].v; if(dy[v]==0) { dy[v]=dx[u]+1; if(X[v]==-1) { flag=1; } else { dx[ X[v] ]=dy[v]+1; Q[rear++]=X[v]; //Q.push(X[v]); } } } } return flag; } bool dfs(int u) { int v; for(int i=head[u];~i;i=e[i].next) { v=e[i].v; if(dx[u]+1!=dy[v])continue; dy[v]=0; if(X[v]==-1||dfs(X[v])) { X[v]=u; Y[u]=v; return 1; } } return 0; } void getans() { //clr(X,-1);clr(Y,-1); FOR(i,0,n)Y[i]=-1; FOR(i,0,m)X[i]=-1; int ret=0; while(bfs()) { //puts("++++"); FOR(i,1,n) { //clr(T,0); if(Y[i]==-1&&dfs(i)) ++ret; } } printf("%d\n\n",ret); } int main() { int cas,t; while(~scanf("%d",&cas)) { FOR(ca,1,cas) { init(); scanf("%d%d",&t,&n); FOR(i,1,n) { scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].dis); p[i].dis*=t; } scanf("%d",&m); _point zt; FOR(i,1,m) { scanf("%d%d",&zt.x,&zt.y); FOR(j,1,n) if(p[j].ok(zt)) { add(j,i); } } printf("Scenario #%d:\n",ca); getans(); } } }