hdu 1150 Machine Schedule

hdu  1150  Machine Schedule                题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1150

图论(第二弹) 匈牙利算法

题目大意:现有A、B两台机器,A有n种工作模式,B有m种工作模式,现给出k个工作,每个工作可由A机器的第i种模式或B机器的第j种模式完成,转换模式需要重启,两台机器的初始状态为0模式,问最少重启几次。

题目分析:确实是个二分图匹配,A、B两个点集的无向图,求最大匹配。

code:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define N 105
int m,n,match[N];
bool g[N][N],mark[N];
bool hungary(int x)
{
	for(int i=0;i<m-1;i++)
	{
		if(g[x][i]&&!mark[i])
		{
			mark[i]=true;
			if(match[i]==-1||hungary(match[i]))
			{
				match[i]=x;
				return true;
			}
		}
	}
	return false;
}
int main()
{
	int i,j,x,y,k,sum;
	while(scanf("%d",&n)!=EOF&&n)
	{
		sum=0;
		memset(g,false,sizeof(g));
		memset(match,-1,sizeof(match));
		scanf("%d%d",&m,&k);
		while(k--)
		{
			scanf("%d%d%d",&j,&x,&y);
			if(x>0&&y>0)g[x-1][y-1]=true;
		}
		for(i=0;i<n-1;i++)
		{
			memset(mark,false,sizeof(mark));
			if(hungary(i))sum++;
		}
		printf("%d\n",sum);
	}
	return 0;
} 
PS:差不多会写了,感觉30%了,再就是学算法本身了……





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值