HDU 1150 Machine Schedule (minimum vertex overwrite)

Source: Internet
Author: User
Machine Schedule

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 3444 accepted submission (s): 1669

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 input5 5 10 0 1 1 1 1 2 2 1 3 3 1 4 4 2 1 5 2 2 2 3 3 2 4 8 3 9 4 3 3 0

 

Sample output3

 

Sourceasia 2002, Beijing (Mainland China)

 

Recommendignatius. L General topic;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 A 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.

Minimum vertex overwrite of a bipartite graph = maximum number of matching

This is the minimum vertex overwrite.

 

Each task creates an edge.

The minimum vertices overwrite means that the minimum vertices can be connected to all edges. This is the minimum point overwrite = the maximum number of binary matching.

 

Note that the initial State of a task is 0, so do not add any side with a single point of 0. Because no cost is required

 

 

 /*  HDU 1150 has 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 A 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. Minimum vertex overwrite = maximum number of matches in a Bipartite Graph. It is equivalent to the smallest vertex that eliminates all edges, so it is the smallest vertex overwrite.  */ # Include <Stdio. h> # Include < String . H> # Include <Algorithm> # Include <Iostream> Using   Namespace  STD;  /* **************************************** * ******************************** // Bipartite Graph Matching (hungaryAlgorithm) // Initialization: G [] [] division of vertices on both sides // establish G [I] [J] to indicate the directed edge of I-> J, it is the matching on the left to the right. If no edge is connected, the value 0 is initialized. // UN indicates the number of vertices on the left to match, and Vn indicates the number of vertices on the right to match. // call: res = Hungary (); Maximum number of matched outputs // advantage: Suitable for dense graphs, DFS find augmented paths, simple and easy to understand // time complexity: O (VE) //************************************** ************************************  */  //  Vertex number starting from 0  Const   Int Maxn = 110  ;  Int UN, vn; // Number of U and V  Int  G [maxn] [maxn];  Int  Linker [maxn];  Bool  Used [maxn];  Bool DFS ( Int U) //  Find the augmented path from the left  {  Int  V;  For (V = 0 ; V <VN; V ++)//  The vertex number starts from 0 and needs to be modified to start from 1.        If (G [u] [v] &! Used [v]) {used [v] = True  ;  If (Linker [v] =- 1 | DFS (linker [v]) {  //  Find zengguang Road, reverse Linker [v] = U;  Return   True ;}}  Return   False ; //  Do not forget this sentence.  }  Int  Hungary (){  Int Res = 0  ;  Int  U; memset (linker, - 1 , Sizeof (Linker ));  For (U = 0 ; U <UN; U ++ ) {Memset (used,  0 , Sizeof  (Used ));  If (DFS (u) RES ++ ;}  Return  Res ;}  //  **************************************** **************************************/  Int Main (){  Int  K;  Int  I, U, V;  While (Scanf ( "  % D  " ,& UN), UN) {scanf (  "  % D  " , & VN ,& K); memset (G,  0 ,Sizeof  (G ));  While (K -- ) {Scanf (  "  % D  " , & I, & U ,& V );  If (U> 0 & Amp; V & gt; 0 ) G [u] [v] = 1 ; //  The initial status is 0. Do not add an edge of 0 at the beginning. } Printf (  "  % D \ n  "  , Hungary ());}  Return   0  ;} 

 

 

 

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.