Machine Schedule
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 12291 |
|
Accepted: 5250 |
Description
As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling problems differ widely in the nature of the constraints that must be satisfied and the type of schedule desired. Here we consider a 2-machine scheduling problem.
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.
Input
The input file for this program consists of several configurations. The first line of one configuration contains three positive integers: n, m (n, m < 100) and k (k < 1000). The following k lines give the constrains of the k jobs, each line is a triple: i, x, y.
The input will be terminated by a line containing a single zero.
Output
The output should be one integer per line, which means the minimal times of restarting machine.
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
题意:有A,B两台机器,A有n个模式,B有m个模式。给定k个任务,每个任务可以选择在A机器的某个模式下工作,也可以选择在B机器的某个模式下工作。问最少要切换多少次模式,才能完成这些工作?初始时A,B均在0模式下。
考虑:当A切换到i模式,那么所有可以在A机器i模式下工作的任务,就可以全部做完。
如果我们把A机器的模式看成n个点,B机器的模式看成m个点,每件任务看成一条边,那么选择A机器的i模式,就相当于和A部的i点相连的边就可以完成作业,于是问题转换为求至少需要多少个点可以覆盖所有边,即最小点覆盖集问题。
而二部图是非常特殊的,二部图的最小点覆盖=最大匹配,Matrix大牛给出了构造性证明。
传送门:Matrix67博客
注意一下:原先AB处在0模式,因此将那些0模式可完成的任务除去。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#define Maxn 110
using namespace std;
int adj[Maxn][Maxn];
int match[Maxn];
int vis[Maxn];
int x,y;
int deep;
bool dfs(int u){
for(int v=1;v<=y;v++){
if(adj[u][v]&&vis[v]!=deep){
vis[v]=deep;
if(match[v]==-1||dfs(match[v])){
match[v]=u;
return true;
}
}
}
return false;
}
int hungary(){
memset(match,-1,sizeof match);
memset(vis,-1,sizeof vis);
int ans=0;
for(int i=1;i<=x;i++){
deep=i;
if(dfs(i)) ans++;
}
return ans;
}
int main()
{
int k,a,b;
while(scanf("%d",&x),x){
scanf("%d%d",&y,&k);
memset(adj,0,sizeof adj);
for(int i=0;i<k;i++){
scanf("%*d%d%d",&a,&b);
if(a*b==0) continue;
adj[a+1][b+1]=1;
}
printf("%d\n",hungary());
}
return 0;
}