由公式二分圖的最小頂點覆蓋數 = 二分圖的最大匹配數知道,要求出把所有的任務完成,但要重啓的次數最少,就是求最小頂點覆蓋數,另外題目中規定如果是0模式時不用重新啓動!!
There are two machines A and B. Machine A has n kinds of working modes, which is called mode_0, mode_1, …, mode_n-1, likewise machine B has m kinds of working modes, mode_0, mode_1, … , mode_m-1. At the beginning they are both work at mode_0.
For k jobs given, each of them can be processed in either one of the two machines in particular mode. For example, job 0 can either be processed in machine A at mode_3 or in machine B at mode_4, job 1 can either be processed in machine A at mode_2 or in machine B at mode_4, and so on. Thus, for job i, the constraint can be represent as a triple (i, x, y), which means it can be processed either in machine A at mode_x, or in machine B at mode_y.
Obviously, to accomplish all the jobs, we need to change the machine's working mode from time to time, but unfortunately, the machine's working mode can only be changed by restarting it manually. By changing the sequence of the jobs and assigning each job to a suitable machine, please write a program to minimize the times of restarting machines.
The input will be terminated by a line containing a single zero.
代碼:
#include<iostream>
using namespace std;
bool map[122][122],visit[122];
int match[122];
int m,n;
bool dfs(int k)
{
int i;
for(i=0;i<n;i++)
{
if(map[k][i] && !visit[i])
{
visit[i]=true;
if(match[i]==-1 || dfs(match[i]))
{
match[i]=k;
return true;
}
}
}
return false;
}
int main()
{
int k,a,b,c;
int s,i;
while(scanf("%d",&m)) //m,n模式。k任務數
{
if(m==0)
break;
scanf("%d%d",&n,&k);
s=0;
memset(map,false,sizeof(map));
memset(match,-1,sizeof(match));
for(i=0;i<k;i++)
{
scanf("%d%d%d",&c,&a,&b);
if(a &&b) //0模式時不用重新啓動
map[a][b]=true;
}
for(i=0;i<m;i++)
{
memset(visit,false,sizeof(visit));
if(dfs(i))
s++;
}
printf("%d\n",s);
}
return 0;
}