Machine Schedule poj1325
Machine Schedule
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 17454 | Accepted: 7327 |
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.
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.
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 10 0 1 1 1 1 2 2 1 3 3 1 4 4 2 1 5 2 2 6 2 3 7 2 4 8 3 3 9 4 3 0
Sample Output
3
题意:这题的题意是说,有两台机器A,B,每个机器有m种模式,对于每一个任务i,可以在模式a[i]或者b[i]上进行,,任务可以以任意的顺序执行,每一次转换模式,机器每台机器就
要重新启动一次,求怎样安排顺序,使得机器重启的次数最少。
这张图很显然是一张二分图,求出他的最小点覆盖,就等价于用尽量少的模式在执行任务。但是这题有一点是需要注意的,刚开始两台机器都处于0模式,因此这个模式要删除掉,也就是不处理。
代码实现:
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 const int MAXN = 2050; 6 int k, n, m, first[MAXN], sign; 7 struct Edge 8 { 9 int to, w, next; 10 } edge[MAXN * 4]; 11 void init() 12 { 13 for(int i = 1; i <= n; i++ ) 14 { 15 first[i] = -1; 16 } 17 sign = 0; 18 } 19 void add_edge(int u, int v, int w) 20 { 21 edge[sign].to = v; 22 edge[sign].w = w; 23 edge[sign].next = first[u]; 24 first[u] = sign++; 25 } 26 int link[MAXN], vis[MAXN]; 27 bool match(int x) 28 { 29 for(int i = first[x]; ~i; i = edge[i].next) 30 { 31 int to = edge[i].to; 32 if(!vis[to]&&(x!=0&&to!=0)) 33 { 34 vis[to] = 1; 35 if(!link[to] || match(link[to])) 36 { 37 link[to] = x; 38 return 1; 39 } 40 } 41 } 42 return 0; 43 } 44 int main() 45 { 46 while(~scanf("%d",&n) && n) 47 { 48 scanf("%d %d",&m, &k); 49 init(); 50 int ans = 0; 51 for(int i = 1; i <= k; i++ ) 52 { 53 int u, v,z; 54 scanf("%d%d%d",&z, &u, &v); 55 add_edge(u, v, 1); 56 } 57 memset(link, 0, sizeof(link)); 58 for(int i = 1; i <= n; i++ ) 59 { 60 memset(vis, 0, sizeof(vis)); 61 if(match(i)) 62 { 63 ans++; 64 } 65 } 66 printf("%d\n", ans); 67 } 68 69 return 0; 70 }