Machine Schedule
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
题目类型:二分图最大匹配
算法分析:本题易知要求的是二分图的最小点覆盖,可以转换成最大匹配来求解。由于完成作业0不需要重启机器,所以最后直接建图求最大匹配即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define i64 long long const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 300 + 66; int edge[maxn][maxn], vis[maxn]; int cx[maxn], cy[maxn], cxlen, cylen; void Init () { for (int i = 1; i < maxn; i++) for (int j = 1; j < maxn; j++) edge[i][j] = INF; } long long dfs (int u) { for (int v = 1; v <= cylen; v++) { if (edge[u][v] < INF && !vis[v]) { vis[v] = 1; if (cy[v] == -1 || dfs (cy[v])) { cy[v] = u; cx[u] = v; return 1LL; } } } return 0LL; } long long MaxMatch () { long long res = 0; memset (cx, -1, sizeof (cx)); memset (cy, -1, sizeof (cy)); for (int i = 1; i <= cxlen; i++) if (cx[i] == -1) { memset (vis, 0, sizeof (vis)); res += dfs (i); } return res; } int main() { // CPPFF; int k; while (cin >> cxlen) { if (cxlen == 0) break; Init (); cin >> cylen >> k; for (int i = 1; i <= k; i++) { int u, a, b; cin >> u >> a >> b; edge[a][b] = 1; } cout << MaxMatch () << endl; } return 0; } |
- « 上一篇:poj1321
- poj1330:下一篇 »