Hdu1150machine schedule (binary matching, minimum vertex overwrite)

Source: Internet
Author: User
Problem descriptionas 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 particle 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.

Obviusly, 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.

Inputthe input file for this program consists of several invocations. 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.

Outputthe output shocould be one integer per line, which means the minimal times of restarting machine.

Sample Input
5 5 100 1 11 1 22 1 33 1 44 2 15 2 26 2 37 2 48 3 39 4 30

Sample output
3. There are two machines A and B and N tasks to run. Each machine has m different modes, and each task runs exactly on one machine. If it runs on machine A, machine A needs to be set to mode XI. If it runs on machine B, machine B needs to be set to mode Yi. Tasks on each machine can be executed in any order, but each machine needs to be restarted once for each conversion mode. Please arrange a machine for each task and arrange the order as little as possible. Problem-solving: each task is an edge, but the zero vertex mode does not need to be included, because at the beginning the machine is the mode 0 and the manual re-tuning mode is not required. Now, you need to complete all the tasks in at least manual mode, that is, the minimum number of vertices overwrites all edges. The minimum vertex overwrite = the maximum matching.
# Include <stdio. h> # include <string. h> int map [105] [105], vist [105], Match [105], n, m; int find (int u) {for (int v = 0; v <m; V ++) if (! Vist [v] & map [u] [v]) {vist [v] = 1; if (Match [v] =-1 | find (Match [v]) {match [v] = u; return 1 ;}} return 0 ;} int main () {int K, A, X, Y; while (scanf ("% d", & N)> 0 & N) {scanf ("% d", & M, & K); For (INT I = 0; I <n; I ++) for (Int J = 0; j <m; j ++) map [I] [J] = 0; For (INT I = 0; I <K; I ++) {scanf ("% d", & A, & X, & Y); If (X & Y) map [x] [Y] = 1; // an edge indicates a task. If any of the modes is 0, you do not need to call this task.} memset (match,-1, sizeof (MATCH )); int ans = 0; For (INT I = 0; I <n; I ++) {memset (vist, 0, sizeof (vist )); ans + = find (I);} printf ("% d \ n", ANS );}}



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.