POJ 1325 Machine Schedule (minimum point coverage && binary graph Max match)

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Links: http://poj.org/problem?id=1325</span>


Description

as we all know, machine scheduling are a very classical problem in computer science and have been studied for a very lon G 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 was machines a and B. Machine A have 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 is both work at Mode_0.

for K jobs given, each of the them can is processed in either one of the one of the both machines in particular mode. For example, job 0 can either is processed in machine A at mode_3 or in machine B at Mode_4, Job 1 can either be processed In machine A is mode_2 or in machine B at Mode_4, and so on. Thus, for Job I, the constraint can is represent as a triple (I, X, y), which means it can be processed either 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 unfortunate Ly, the machine's working mode can only is 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 <) and K (K < 1000). The following k lines give the constrains of the K jobs, each of which is a triple:i, X, Y.

The input would be terminated to a line containing a single zero.


Output

The output should is 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


Test instructions
There are two machines A and b, each with N and m different modes, K work, each of which can be processed in a particular mode of the two machines.
For example, job0 can be processed either in the number 3rd mode of a machine or in the number 4th mode of the B machine.

The machine's working mode changes can only be restarted by manual operation. By changing the order of work and assigning each job to the right machine, the number of times to restart the machine can be minimized.
The task is to calculate the minimum number of times. Initially, both machines are running in mode No. 0.

Ideas:
Turn each task into a line, assuming that task I processes a a[x] point on a machine, on a B machine for b[y] points, connect a[x] and B[y],
Cover all edges with the fewest points in a machine and B machine (all tasks are done in a minimal mode).
This is the minimum point coverage problem, according to the K?nig theorem (the maximum number of matches in a binary graph equals the minimum point coverage in the graph) is the maximum match of the binary graph,
And then use the Hungarian algorithm directly, even if the maximum number of matches, to note that the initial is in the No. 0 number mode,
So if A or B machine has at least one of them in the No. 0 mode, there is no need to restart the machine, so the building is not built in 0.


Password:

#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]&& amp;!            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", &AMP;MC, &AMP;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;}


POJ 1325 Machine Schedule (minimum point coverage && binary graph Max match)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.