poj 2536 Gopher II 二分匹配应用 匈牙利算法

题意:有某种动物n个,以及m个洞穴。给定n个动物坐标,以及m个洞穴坐标。每个动物有相同的运动速度v,单位是m/s。在老鹰来时,如果不能在s秒内(含s秒)躲进洞里,将会被吃,求怎么分配动物及洞穴,使得尽量少的动物被抓。

思路:建立结点,左边为动物,右边为洞穴,对于左边任意一个动物,若能够在s秒内躲进右边任意一个洞穴,则之间连一条边。接下来就是求最大匹配,用总数减去最大匹配数,就是最少被抓数量。

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<iostream>
using namespace std;
const int maxn = 400;
struct edge
{
	int from,to;
};
struct Pt
{
	double x,y;
}pt[maxn];
vector<edge> edges;
vector<int> g[maxn];
int match[maxn],check[maxn];
int n,m,s,v;
bool dfs(int u)
{
	for(int i=0;i<g[u].size();i++)
	{
		int v=edges[g[u][i]].to;
		if(!check[v])
		{
			check[v]=1;
			if(match[v]==-1||dfs(match[v]))
			{
				match[u]=v;
				match[v]=u;
				return true;
			}
		}
		
	}
	return false;
}
int Hungarian()
{
	int ans=0;
	memset(match,-1,sizeof(match));
	for(int i=1;i<=n;i++)
	{
		if(match[i]==-1)
		{
			memset(check,0,sizeof(check));
			if(dfs(i))ans++;
		}
		
	}
	return ans;
}
double dist(int i,int j)
{
	return sqrt((pt[i].x-pt[j].x)*(pt[i].x-pt[j].x)+
	(pt[i].y-pt[j].y)*(pt[i].y-pt[j].y));
}
void addedge(int from,int to)
{
	edges.push_back(edge{from,to});
	edges.push_back(edge{to,from});
	int m=edges.size();
	g[from].push_back(m-2);
	g[to].push_back(m-1);
}
int main()
{
	//freopen("in.txt","r",stdin);
	while(scanf("%d%d%d%d",&n,&m,&s,&v)!=EOF)
	{
		double x,y;
		edges.clear();
		for(int i=1;i<=n+m;i++)g[i].clear();
		int i=1;
		for(;i<=n;i++)
		{
			cin>>x>>y;
			pt[i].x=x;
			pt[i].y=y;
		}
		m=n+m;
		for(i=n+1;i<=m;i++)
		{
			cin>>x>>y;
			pt[i].x=x;
			pt[i].y=y;
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=n+1;j<=m;j++)
			{
				double dt=dist(i,j)/(v*1.0);
				if(dt<=s)
				{
					addedge(i,j);
				}
			}
		}
		int ans=Hungarian();
		printf("%d\n",n-ans);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值