https://vjudge.net/contest/238849#problem/F
题目的意思就是再有t秒就要下雨了,宾客去拿伞,问最多有多少宾客可以拿到伞
二分匹配,用匈牙利算法一定会超时,就用Hk算法
#include<stdio.h>
#include<string.h>
#include<string>
#include<queue>
#include<iostream>
#include<algorithm>
const int inf=0x3f3f3f3f;
using namespace std;
int g[3010][3010];
int dx[3010];
int dy[3010];
int linex[3010];
int liney[3010];
int vis[3010];
int dis;
int t,n,m;
struct node1
{
int x;
int y;
int v;
}people[3010];
struct node2
{
int x;
int y;
}umbrella[3010];
int abs(int a,int b)
{
if(a>b)
return a-b;
else
return b-a;
}
int distance(node1 a,node2 b)
{
return abs(a.x-b.x)+abs(a.y-b.y);
}
bool searchpath()
{
queue<int>q;
dis=inf;
memset(dx,-1,sizeof(dx));
memset(dy,-1,sizeof(dy));
for(int i=1;i<=n;i++)
{
if(linex[i]==-1)
{
q.push(i);
dx[i]=0;
}
}
while(!q.empty())
{
int u=q.front();
q.pop();
if(dx[u]>dis)break;
for(int i=1;i<=m;i++)
{
if(g[u][i]&&dy[i]==-1)
{
dy[i]=dx[u]+1;
if(liney[i]==-1)dis=dy[i];
else
{
dx[liney[i]]=dy[i]+1;
q.push(liney[i]);
}
}
}
}
return dis!=inf;
}
int find1(int v)
{
for(int i=1;i<=m;i++)
{
if(!vis[i]&&g[v][i]&&dy[i]==dx[v]+1)
{
vis[i]=1;
if(liney[i]!=-1&&dy[i]==dis)continue;
if(liney[i]==-1||find1(liney[i]))
{
liney[i]=v;
linex[v]=i;
return 1;
}
}
}
return 0;
}
int maxmatch()
{
int ans=0;
memset(linex,-1,sizeof(linex));
memset(liney,-1,sizeof(liney));
while(searchpath())
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
if(linex[i]==-1)
{
ans+=find1(i);
}
}
}
return ans;
}
int main()
{
int T;
int r=0;
scanf("%d",&T);
while(T--)
{
memset(g,0,sizeof(g));
scanf("%d%d",&t,&n);
for(int i=1;i<=n;i++)
scanf("%d%d%d",&people[i].x,&people[i].y,&people[i].v);
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d%d",&umbrella[i].x,&umbrella[i].y);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(distance(people[i],umbrella[j])/people[i].v<=t)//如果在t时间内能拿到伞就将人伞联系在一起
g[i][j]=1;
}
}
printf("Scenario #%d:\n%d\n\n",++r,maxmatch());
}
}