Machine Schedule
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 13071 |
|
Accepted: 5575 |
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种工作模式,分别为 mode_0,mode_1,mode_2.....,机器B有 m种工作模式,分别为 mode_0,mode_1,mode_2.....。刚开始A,B的工作模式都是mode_0。
给定K个任务,表示为(i ,x,y),意思是作业 i 可以工作在机器A的mode_x模式或者机器B的mode_y的模式。
为了完成所有的工作,必须时不时的切换机器的工作模式,但机器工作模式的切换只能通过重启机器完成,问你最少重启多少次机器,才能把工作分配完。
解析:
一看有A ,B种机器,再根据题意,两种机器有匹配关系,我们首先构造二分图,把A的n个mode和B的m个mode看做图的顶点,如果某个任务可以在A的mode_i 或B的mode_j 上完成,则从Ai 到 Bj连一条边,这样就构成了二分图。
由题意可知,本意要求的是二分图的最小点覆盖集问题,即最小的顶点集合,“覆盖”所有的边,可以转化成二分图的最大匹配问题。
二分图的最小点覆盖数 == 最大匹配数。
另外要注意,机器A 和机器B最初都是在mode_0,所以对那些可以在机器A的mode_0或者机器B的模式_0工作的作业,在完成这些作业时是不需要重启机器的。一开始没考虑这里,贡献了一次wa。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 110
using namespace std;
int map[maxn][maxn];
int used[110];
int link[maxn];
int n, m, k;
void getmap(){
while(k--){
int c, a, b;
scanf("%d%d%d", &c, &a, &b);
if(a == 0 || b == 0) continue;
map[a][b] = 1;
}
}
bool dfs(int x){
for(int i = 0; i < m; ++i){
if(!used[i] && map[x][i]){
used[i] = 1;
if(link[i] == -1 || dfs(link[i])){
link[i] = x;
return true;
}
}
}
return false;
}
int hungary(){
int ans = 0;
memset(link, -1, sizeof(link));
for(int i = 0; i < n; ++i){
memset(used, 0, sizeof(used));
if(dfs(i))
ans++;
}
return ans;
}
int main (){
while(scanf("%d", &n), n){
scanf("%d%d", &m, &k);
memset(map, 0, sizeof(map));
getmap();
int sum = 0;
sum = hungary();
printf("%d\n", sum);
}
return 0;
}