POJ 1325 Machine Schedule (最小点覆盖 && 二分图最大匹配)

来源:互联网 发布:手机拍照定位软件 编辑:程序博客网 时间:2024/05/22 08:24

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">鏈接: http://poj.org/problem?id=1325</span>


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,分别有n种和m种不同的模式,有k个工作,每个工作都可以在那两个机器的某种特定的模式下处理。
如job0既可以在A机器的3号模式下处理,也可以在B机器的4号模式下处理。

机器的工作模式改变只能通过人工来重启。通过改变工作的顺序,和分配每个工作给合适的机器可以减少重启机器的次数达到最小。
任务就是计算那个最小的次数。初始时两台机器都运行在0号模式下。

思路:
把每个任务化为一条线,假设任务i在A机器上处理的模式为A[x]点,在B机器上为B[y]点,连接A[x]和B[y],
用A机器和B机器中最少的点覆盖所有的边(用最少的模式完成所有的任务)。
这是最小点覆盖问题,根据König定理(一个二分图中的最大匹配数等于这个图中的最小点覆盖数)就是求的二分图的最大匹配,
然后再用匈牙利算法直接就算出最大匹配数了,要注意的是初始是在0号模式下,
所以如果A或B机器其中至少有个在0号模式下时就不用重启机器了,所以建图的时候没有把0建进去。


代碼:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>#define MAXN 100#define RST(N)memset(N, 0, sizeof(N))using namespace std;int m, n, k, Mc, Md, Me;bool map[MAXN][MAXN], vis[MAXN];int link[MAXN], res;bool find(int x){    for(int i=1; i<n; i++) if(map[x][i]&&!vis[i]) {            vis[i] = true;            if(link[i] == 0 || find(link[i])) {                link [i] = x ;                return true ;            }    }    return false;}void Init(){    RST(map), RST(link);    for(int i=0; i<k; i++) {        scanf("%d %d %d", &Mc, &Md, &Me);        if(Md == 0 || Me == 0) continue;        map[Md][Me] = true;    }    res = 0;}int main(){    while (~scanf("%d %d %d", &m, &n, &k) && m) {        Init();        for(int i=1; i<m; i++) {            RST(vis);            if(find(i)) res++;        }        printf("%d\n", res);    }    return 0;}


0 0
原创粉丝点击