Problem : Machine Schedule
Description : 有A和B两种类型的机器,现在有一些工作,它可以在A和B中的任何一种机器中工作,然而,机器的开启是需要代价的,因此A中的第0个机器和B中的第0个机器一开始就是开着的,现在问你至少需要开启多少台机器才能使得所有的工作都被完成。
Solution : 二分图的最小点覆盖,点就是AB中的机器,而边就是一个工作在AB机器中的使用,于是这个最小点覆盖的意义就是用最少机器来使得所有的工作都能在机器中完成,建图完毕。但是这个题目要注意的是,机器0始终是开着的。那么如果边集中有0这个端点,那么我们就要去掉,因为这个工作不需要开启新的机器了,它在0机器上完成工作就可以了。如果对最小点覆盖不清楚可以参考下面的引用。
Code(C++) :
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int F=1000+5;
int n,m,k;
vector<int> edge[F];
bool used[F];
int belong[F];
bool dfs(int s)
{
for(int i=0;i<edge[s].size();i++){
int end=edge[s].at(i);
if(used[end])
continue;
used[end]=true;
if(belong[end]==-1||dfs(belong[end])){
belong[end]=s;
return true;
}
}
return false;
}
int main()
{
//freopen("in.data","r",stdin);
while(scanf("%d",&n),n){
scanf("%d%d",&m,&k);
int x,y;
for(int i=0;i<F;i++)
edge[i].clear();
memset(belong,-1,sizeof(belong));
for(int i=0;i<k;i++){
scanf("%*d%d%d",&x,&y);
if(x*y)
edge[x].push_back(y);
}
int sum=0;
for(int i=1;i<n;i++){
memset(used,false,sizeof(used));
if(dfs(i))
++sum;
}
printf("%d\n",sum);
}
return 0;
}