POJ1325 Machine Schedule

Machine Schedule
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 15700   Accepted: 6734

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

Source

 
【题解】
每个任务要么用A的某个模式要么用B的某个模式,二分图
若其中某个模式为0,不连边,否则连边
最小点覆盖即可
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 
 6 inline void read(int &x)
 7 {
 8     x = 0;char ch = getchar(), c = ch;
 9     while(ch < '0' || ch > '9') c = ch, ch = getchar();
10     while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
11     if(c == '-')x = -x;
12 }
13 
14 const int MAXN = 100 + 10;
15 const int MAXK = 1000 + 10;
16 
17 int g[MAXN][MAXN], lk[MAXN], b[MAXN], n, m, k, tmp1, tmp2;
18 
19 int dfs(int u)
20 {
21     for(register int i = 1;i <= m;++ i)
22     {
23         if(!b[i] && g[u][i])
24         {
25             b[i] = 1;
26             if(lk[i] == -1 || dfs(lk[i]))
27             {
28                 lk[i] = u;
29                 return 1;
30             }
31         }
32     }
33     return 0;
34 }
35 
36 int xiongyali()
37 {
38     int ans = 0;
39     memset(lk, -1, sizeof(lk));
40     for(register int i = 1;i <= n;++ i)
41     {
42         memset(b, 0, sizeof(b));
43         ans += dfs(i);
44     }
45     return ans;
46 }
47 
48 int main()
49 {
50     freopen("data.txt", "r", stdin);
51     while(scanf("%d %d %d", &n, &m, &k) != EOF && n)
52     {
53         memset(g, 0, sizeof(g));
54         for(register int i = 1;i <= k;++ i)
55         {
56             read(tmp1), read(tmp1), read(tmp2);
57             if(tmp1 == 0 || tmp2 == 0) continue;
58             g[tmp1][tmp2] = 1;
59         }    
60         printf("%d
", xiongyali());
61     }
62     return 0;
63 }
POJ1325