Main topic:
There are two machines A and B as well as k tasks that need to be run. A machine has n different modes, B machines have m different modes, and each task is running on exactly one 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 properly arrange a machine for each task and arrange the order reasonably, so that the machine restarts as few times as possible.
Exercises
The n mode of machine A is used as the left part of the binary graph, and the M mode of machine B is the right part of the binary graph, and if a task can use machine A's Mode Xi can also be completed using machine B's Mode Yi, then connect xi,yi.
To make the machine restart the fewest number of times to complete all the tasks, the problem is converted to take the minimum amount of points from this n+m to cover all edges.
Also according to the properties of the binary graph: Minimum point coverage = maximum number of matches, you can use the Hungarian algorithm to find out the answer
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <stack>6#include <queue>7#include <cstring>8 #definePau Putchar (")9 #defineent putchar (' \ n ')Ten #defineMSE (A, B) memset (A,b,sizeof (a)) One #defineren (x) for (TED*E=FCH[X];E;E=E->NXT) A #defineRep (i,s,t) for (int i=s,__=t;i<=__;i++) - #defineDwn (i,s,t) for (int i=s,__=t;i>=__;i--) - using namespacestd; the Const intmaxn= $+Ten, maxm=40000+Ten; - intLNK[MAXN];BOOLVIS[MAXN]; - structted{intX,Y;TED*NXT;} adj[maxm],*fch[maxn],*ms=adj; - voidAddintXintY) {*ms= (Ted) {x,y,fch[x]};fch[x]=ms++;return;}intn,m,k; + BOOLMatchintx) { -ren (x) {intv=e->y;if(!vis[v]) {vis[v]=true; + if(!lnk[v]| | Match (Lnk[v])) {lnk[v]=x;return true;} A } at}return false; - } - intHungary () { -MSE (LNK,false);intans=0; Rep (I,1, N) {MSE (VIS,false);if(Match (i)) ans++;}returnans; - } -InlineintRead () { in intx=0;BOOLsig=true;CharCh=GetChar (); - for(;! IsDigit (CH); Ch=getchar ())if(ch=='-') sig=false; to for(; isdigit (ch); Ch=getchar ()) x=Ten*x+ch-'0';returnsig?x:-x; + } -InlinevoidWriteintx) { the if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; * intlen=0;Static intbuf[ -]; while(x) buf[len++]=x%Ten, x/=Ten; $ for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return;Panax Notoginseng } - intMain () { the while(true){ +N=read ();if(!n) Break; AMSE (fch,null); ms=adj; theM=read (); K=read ();intx, y; +Rep (I,1, K) { -Read (); X=read (); Y=read ();if(x&&y) Add (x, y); $ }write (Hungary ()); ENT; $ } - return 0; -}
HDU 1150 Machine Schedule