题目意思:有两台机器A和B以及N个需要运行的任务。每台机器有M种不同的模式,而每个任务都恰好在一台机器上运行。如果它在机器A上运行,则机器A需要设置为模式ai,如果它在机器B上运行,则机器A需要设置为模式bi。每台机器上的任务可以按照任意顺序执行,但是每台机器每转换一次模式需要重启一次。请合理为每个任务安排一台机器并合理安排顺序,使得机器重启次数尽量少。
解题思路:这个题可转化成求二分图的最大匹配数,最小覆盖数=最大匹配。用匈牙利算法。
代码:
#include<iostream>
using namespace std;
const int MAX = 102;
bool g[MAX][MAX];
int match[MAX];
bool used[MAX];
int n, m;
bool search(int u)
{
for (int i=1;i<m;i++)
{
if (g[u][i]&&!used[i])
{
used[i]=1;
if (match[i]==-1||search(match[i]))
{
match[i]=u;
return true;
}
}
}
return false;
}
int hungary()
{
int cnt = 0;
memset(match, -1, sizeof(match));
for(int i= 1; i<n; i++)
{
memset(used,0, sizeof(used));
if(search(i))
cnt++;
}
return cnt;
}
int main()
{
int k;
while(cin>>n,n)
{
cin>>m>>k;
memset(g,false, sizeof(g));
for(int i=0;i<k;i++)
{
int v1, v2;
cin>>v1>>v1>>v2;
if(v1&&v2)
g[v1][v2]=true;
}
cout<<hungary()<<endl;
}
return 0;
}