题目链接:http://poj.org/problem?id=1325
Description
Input
Output
Sample Input
5 5 10 0 1 1 1 1 2 2 1 3 3 1 4 4 2 1 5 2 2 6 2 3 7 2 4 8 3 3 9 4 3 0
Sample Output
3
Source
题目大意;有两台机器A和B以及N个需要运行的任务。每台机器有M种不同的模式,而每个任务都恰好在一台机器上运行。如果它在机器A上运行,则机器A需要设置为模式xi,如果它在机器B上运行,则机器A需要设置为模式yi。每台机器上的任务可以按照任意顺序执行,但是每台机器每转换一次模式需要重启一次。请合理为每个任务安排一台机器并合理安排顺序,使得机器重启次数尽量少。
二分图的最小顶点覆盖数=最大匹配数
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 117;
int LN, RN;
int g[MAXN][MAXN], linker[MAXN];
bool used[MAXN];
int dfs(int L)
{
for(int R = 0; R < RN; R++)
{
if(g[L][R] && !used[R])
{
used[R] = true;
if(linker[R] == -1 || dfs(linker[R]))
{
linker[R] = L;
return 1;
}
}
}
return 0;
}
int hungary()
{
int res = 0;
memset(linker,-1,sizeof(linker));
for(int L = 0; L < LN; L++)
{
memset(used,0,sizeof(used));
if(dfs(L))
res++;
}
return res;
}
int main()
{
int i, L, R;
int res;
int k;
while(~scanf("%d",&LN) && LN)
{
scanf("%d%d",&RN,&k);
memset(g,0,sizeof(g));
for(int j = 0; j < k; j++)
{
scanf("%d%d%d",&i,&L,&R);
if(L > 0 && R > 0)
g[L][R] = 1;
}
res = hungary();
printf("%d\n",res);
}
return 0;
}
POJ 1325 Machine Schedule(二分匹配 最小点覆盖)
原文地址:http://blog.csdn.net/u012860063/article/details/39206999