hdu 2389 Rain on your Parade 二分匹配 HK算法

时间:2021-04-06 06:11:45

题目链接


匈牙利算法超时 


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = 3005;
const int inf = 1<<30;
int nx,ny,t;
int dist;
bool map[maxn][maxn],vis[maxn];
int cx[maxn],cy[maxn];
int dx[maxn],dy[maxn];
struct node
{
int x,y,r;
}man[maxn];
struct node1
{
int x,y;
}unb[maxn];
void readData()
{
int dis;
scanf("%d%d",&t,&nx);
for( int i = 1; i <= nx; i ++ ){
scanf("%d%d%d",&man[i].x,&man[i].y,&man[i].r);
man[i].r *= t;
}
scanf("%d",&ny);
for( int i = 1; i <= ny; i ++ ){
scanf("%d%d",&unb[i].x,&unb[i].y);
}
for( int i = 1; i <= nx; i ++ ){
for( int j = 1; j <= ny; j ++ ){
dis = (man[i].x-unb[j].x)*(man[i].x-unb[j].x) + (man[i].y-unb[j].y)*(man[i].y-unb[j].y);
if( dis <= man[i].r*man[i].r )
map[i][j] = true;
else
map[i][j] = false;
}
}
}

//Hopcroft-Karp算法 有点类似dinic 都是先对图BFS分层再沿层数DFS找增广路
//***************************************************************************
bool searchPath() //BFS 对二分图分层
{
dist = inf;
queue<int>que;
memset( dx,-1,sizeof(dx) );
memset( dy,-1,sizeof(dy) );
for( int i = 1; i <= nx; i ++ ){ //找到x集合所有未被匹配的点压入队列中
if( cx[i] == -1 ){
que.push(i);
dx[i] = 0;
}
}
while( !que.empty() ){
int u = que.front(); que.pop();
if( dx[u] > dist ) //只分层到第一个找个的可匹配点层数
break;
for( int v = 1; v <= ny; v ++ ) {
if( map[u][v] && dy[v] == -1 ){
dy[v] = dx[u] + 1;
if( cy[v] == -1 ) //找个一个可以匹配点 标记分层的层数
dist = dy[v];
else{
dx[cy[v]] = dy[v] + 1;
que.push( cy[v] );
}
}
}
}
return dist != inf;
}
bool findPath( int u ) //沿着层数DFS
{
for( int v = 1; v <= ny; v ++ ){
if( map[u][v] && !vis[v] && dy[v] == dx[u] + 1 ){
vis[v] = true;
if( cy[v] != -1 && dy[v] == dist ) //如果v已经有匹配了且v的层数为dist( 最大层数为dist 所以v原来匹配的不可能再匹配 )
continue;
if( cy[v] == -1 || findPath( cy[v] ) ){ //如果v未匹配就跟u匹配v 否则看v原来匹配的是否还能跟其他的匹配 能就跟u匹配 不能就不匹配
cy[v] = u;
cx[u] = v;
return true;
}
}
}
return false;
}
int HK_MaxMatch()
{
int ans = 0;
memset( cx,-1,sizeof(cx) );
memset( cy,-1,sizeof(cy) );
while( searchPath() ){ //分层 + 判断是否还有未匹配点
memset( vis,0,sizeof(vis) );
for( int i = 1; i <= nx; i ++ ){
if( cx[i] == -1 )
ans += findPath(i);
}
}
return ans;
}
//***************************************************************************

int main()
{
//freopen("data.txt","r",stdin);
int c,cas = 1;
scanf("%d",&c);
while( c-- ){
readData();
printf("Scenario #%d:\n%d\n",cas++,HK_MaxMatch() );
puts("");
}
return 0;
}