集训队专题(6)1000 Machine Schedule

来源:互联网 发布:数据加密标准des是对称 编辑:程序博客网 时间:2024/05/22 12:10

Machine Schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7440    Accepted Submission(s): 3721


Problem Description
As 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 particular 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.

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

Input
The input file for this program consists of several configurations. 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.
 

Output
The output should 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
 

Source
Asia 2002, Beijing (Mainland China)
 

题意:有两台机器A和B以及N个需要运行的任务。每台机器有M种不同的模式,而每个任务都恰好在一台机器上运行。如果它在机器A上运行,则机器A需要设置为模式xi,如果它在机器B上运行,则机器A需要设置为模式yi。每台机器上的任务可以按照任意顺序执行,但是每台机器每转换一次模式需要重启一次。请合理为每个任务安排一台机器并合理安排顺序,使得机器重启次数尽量少。

此题从题意上由于分成了两个机器,很明显就是一个二分图的题,有关二分图的做法需要用到的就是匈牙利算法,至于匈牙利算法的具体介绍,因为在百度百科上就有,小编这里也就不做详细的介绍,在网上看别人博客上解释在男女搭配问题上的匈牙利算法就是有机会就上,没机会就创造机会上……挺形象的。对匈牙利算法的具体实现用到的是DFS或者是BFS,DFS代码简洁,BFS虽然代码冗长但效率更高,这里小编我用DFS

const int maxn = 100+10;int uN,vN;int g[maxn][maxn],linker[maxn];bool used[maxn];bool dfs(int u){int v;for(v=0; v<vN; v++)if(g[u][v] && !used[v]){used[v] = true;if(linker[v]==-1 || dfs(linker[v])){linker[v] = u;return true;}}return false;}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;}
注意到我们在DFS里的if的判断条件(linker[v]==-1 || dfs(linker[v])),这里linker[v]==-1条件写到前面是有原因的,因为我们的交替路是一个匹配点一个未匹配点,虽然交换位置不会导致结果的错误,但因为||这个条件子啊第一个条件正确的情况下就不会运行第二个条件,所以这样的顺序是可以加高运行效率的,小编这里做了个实验,在交换顺序的情况下的结果比较


很明显在时间上是有区别的。优化就是在这些小细节上体现的。

AC代码:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100+10;int uN,vN;int g[maxn][maxn],linker[maxn];bool used[maxn];bool dfs(int u){int v;for(v=0; v<vN; v++)if(g[u][v] && !used[v]){used[v] = true;if(linker[v]==-1 || dfs(linker[v])){linker[v] = u;return true;}}return false;}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%d",&vN,&k);memset(g,0,sizeof(g));while(k--){scanf("%d%d%d",&i,&u,&v);if(u!=0 && v!=0) g[u][v] = 1;}printf("%d\n",hungary());}return 0;}


0 0