Test instructions: There are two machines, machines A and B, they have various modes 1~n and 1~m, and now there are jobs that need to be done in a specific machine and a specific mode, and the mode of operation required for JOB1 on A and B may be different. Two machines start in 0 mode (without such a task), a machine if you want to switch mode can only reboot (that is, in 0 mode to switch right), now has a bunch of jobs, how many times to restart the machine to complete the task.
Ideas:
Analysis, it is clear that only requires the number of restarts, then time-independent (can be done entirely by a machine to work), as long as the same mode of the task can be executed in one piece will not be restarted to switch mode, so the pattern of the task is only 1 times the restart. However, the two machines may be restarted less often, such as the 1 mode B can perform 2 tasks, but these two tasks need to be executed in the two modes on the A machine, and similarly a may also have such a task as B.
The goal is to make one mode of a machine as much as possible to kill the multiple modes of another machine (the other tasks no longer contain these patterns after being killed). In fact, we want to ask for the minimum coverage: Select some points (each point represents 1 modes) to cover all the edges (each side represents 1 tasks). According to the Konig theorem, the minimum coverage number = maximum number of matches. Then ask for the maximum match on the line, you can use the Hungarian algorithm, less code. (Hungarian algorithm to see "Knowledge of Popular Science" classification)
Directly into the model of male-female pairing, A's pattern is all male, B's pattern is female.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intn= the;4 intN, M, K, R, a, B;5 6 BOOLMapp[n][n];//Matrix7 BOOLMatch[n];//used to find the path, traversed by the point is marked8 BOOLVis[n];//have the women been tagged?9 intGirl[n];//Suppose B is a woman.Ten One A //Suppose A is male and B is female - intFindintX//find a female object for x - { the for(intI=1; i<=m; i++)//Scan all Sister - { - if(Mapp[x][i] &&!match[i])//know, haven't tried to help this sister to find another object - { +match[i]=1;//This girl has tried to help her find her target. - if(!vis[i] | |find (Girl[i])) + { AGirl[i]=x;//If I can find another object for her boyfriend, then this sister is mine. atvis[i]=1; - return true; - } - } - } - return false; in } - to intHungary () + { - intCnt=0; the for(intI=1; i<=n; i++) * { $memset (Match,0,sizeof(Match));Panax Notoginseng if(Find (i)) cnt++;//another match. - } the returnCNT; + } A the + intMain () - { $Freopen ("Input.txt","R", stdin); $ while(SCANF ("%d",&N), N) - { -scanf"%d%d",&m,&k); thememset (MAPP,0,sizeof(MAPP)); -memset (Vis,0,sizeof(Vis));WuyiMemset (Girl,0,sizeof(Girl)); the for(intI=0; i<k; i++) - { Wuscanf"%d%d%d",&r,&a,&b); - //if (a>0&&b>0) Aboutmapp[a][b]=1;//as a forward side, because the numbers of men and women may be the same $ } -printf"%d\n", Hungary ());//Hungarian Algorithm - } - return 0; A}
AC Code
HDU 1150 Machine Schedule (minimum coverage, max matching Ann City, graph theory)