ZOJ 1364 Machine Schedule (binary graph Max match)

Source: Internet
Author: User

Test instructions Machine Scheduling problem There are two machines, b A has n mode of operation 0...n-1 A M mode of operation 0 ... M-1 and then a K-task to do each task can be done with a machine mode I or B machine mode J to complete the machine is in mode 0 each change mode to restart Ask to complete all task machine Restart at least how many times


The maximal matching problem of the most basic binary graphs for each task can form a two-point graph between I and J. Then each task can correspond to an edge. So now it's time to find the fewest points so that the points can cover all of the edge-click overlays and because of the number of point overlays in the binary graph = match number So that's the bare one. The maximum matching problem of two points is that both sides have no more than 100 direct DFS augmentation on the line.

#include <bits/stdc++.h>using namespace std;const int n = 105;int g[n][n], a[n], b[n], Vis[n];int N, M, K, Ans;int D FS (int i)//dfs augmentation {for (int j = 1; j < m; ++j) {if (G[i][j] &&!vis[j]) {vis[j            ] = 1;                if (b[j] = = 1 | | DFS (B[J]) {//Machine a mode I and machine B mode J match A[i] = j;                B[J] = i;            return 1; }}} return 0;}    int main () {int u, V, t;        while (scanf ("%d", &n), N) {memset (g, 0, sizeof (g));        scanf ("%d%d", &m, &k);            for (int i = 0; i < K; ++i) {scanf ("%d%d%d", &t, &u, &v);        G[U][V] = 1;        } ans = 0;        Memset (A,-1, sizeof (a));        memset (b,-1, sizeof (b));            Status 0 does not require a restart so it is possible to ignore 0 for (int i = 1; i < n; ++i) {if (a[i] = =-1)//i not matched with I as the starting point to find an augmented road                {memset (Vis, 0, sizeof (VIS)); Ans += DFS (i);    }} printf ("%d\n", ans); } return 0;} Last modified:2015-07-10 14:50

Machine Schedule Time limit: 2 Seconds Memory Limit: 65536 KB

As we all know, machine scheduling are a very classical problem in computer science and have been studied for a very long hi Story. 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 is-machines a and B. Machine A have n kinds of working modes, which is called Mode_0, Mode_1,??, mode_n-1, like Wise machine B have 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 unfortunately, 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




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ZOJ 1364 Machine Schedule (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.