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 was machines a and B. Machine A have n kinds of working modes, which is called Mode_0, Mode_1, ..., mode_n-1, Likew Ise 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 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.
Inputthe 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.
Outputthe output should is 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
Test Instructions : There are two machines A and B and N tasks that need to be run. Each machine has a different m mode, and each task runs exactly on a single machine. If it is running on machine A, then machine a needs to be set to mode XI, and if it is running on machine B, then machine a needs to be set to Mode Yi. the tasks on each machine can be executed in any order, but each machine will need to be restarted every time the mode is converted. Please reasonable for each task to arrange a machine and reasonable order, so that the number of machines restart as little as possible (Kuangbin big guy's authoritative explanation, relatively easy to understand, borrow a bit, hope big guy Mo blame AH).
Idea: To build an edge between the two modes of each task, which obviously constitutes a binary graph, now we need to minimize the number of restarts, which is equivalent to the minimum point coverage of the graph.
This problem can be easily made based on the characteristics of the binary graph-minimum point coverage = maximum number of matches. However, because the initial state is zero, it is not added if there is a pattern of 0 edges.
The code is as follows:
#include <cstdio>#include<cstring>#include<iostream>using namespacestd;intmatch[ the][ the],vis[ the],n,m; intye[ the];BOOLFindintx) { for(intI=1; i<=m;i++) { if(match[x][i]==1&&!vis[i])//If there is an edge of x to I and the edge is not matched {Vis[i]=1;//Match this edge, set the edge to be accessedif(ye[i]==-1)//If the edge does not have a matching edge, set X as the matching edge of the edge {Ye[i]=x; return true; } Else { if(Find (ye[i]) = =true)//If the edge is matched, find the edge Ye[i] can match the other edges, if possible, empty the position of I {Ye[i]=x; return true; } } } } return false;}intMain () {intK,i,x,y; intans; while(~SCANF ("%d",&N)) {ans=0; memset (Match,0,sizeof(match)); Memset (Ye,-1,sizeof(Ye)); if(n==0) { Break; } scanf ("%d%d",&m,&k); while(k--) {scanf ("%d%d%d",&i,&x,&y); Match[x][y]=1; } for(intj=1; j<=n;j++) {memset (Vis,0,sizeof(VIS)); if(Find (j) = =true) {ans++; }} printf ("%d\n", ans); } return 0;}
Brush the questions every day, body stick!
HDU1150 Machine Schedule (two-figure maximum matching, minimum-point coverage)