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%了,再就是学算法本身了……