码迷,mamicode.com
首页 > 编程语言 > 详细

Poj1325 Machine Schedule 最大二分图匹配 匈牙利算法

时间:2016-08-18 12:58:48      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

 

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

求最小覆盖
技术分享
 1 #include <iostream>
 2 #include <sstream>
 3 #include <fstream>
 4 #include <string>
 5 #include <vector>
 6 #include <deque>
 7 #include <queue>
 8 #include <stack>
 9 #include <set>
10 #include <map>
11 #include <algorithm>
12 #include <functional>
13 #include <utility>
14 #include <bitset>
15 #include <cmath>
16 #include <cstdlib>
17 #include <ctime>
18 #include <cstdio>
19 #include <cstring>
20 #define FOR(i, a, b)  for(int i = (a); i <= (b); i++)
21 #define RE(i, n) FOR(i, 1, n)
22 #define FORP(i, a, b) for(int i = (a); i >= (b); i--)
23 #define REP(i, n) for(int i = 0; i <(n); ++i)
24 #define SZ(x) ((int)(x).size )
25 #define ALL(x) (x).begin(), (x.end())
26 #define MSET(a, x) memset(a, x, sizeof(a))
27 using namespace std;
28 
29 
30 typedef long long int ll;
31 typedef pair<int, int> P;
32 int read() {
33     int x=0,f=1;
34     char ch=getchar();
35     while(ch<0||ch>9) {
36         if(ch==-)f=-1;
37         ch=getchar();
38     }
39     while(ch>=0&&ch<=9) {
40         x=x*10+ch-0;
41         ch=getchar();
42     }
43     return x*f;
44 }
45 const double pi=3.14159265358979323846264338327950288L;
46 const double eps=1e-6;
47 const int mod = 1e9 + 7;
48 const int INF = 0x3f3f3f3f;
49 const int MAXN = 1005;
50 const int xi[] = {0, 0, 1, -1};
51 const int yi[] = {1, -1, 0, 0};
52 
53 
54 int used[MAXN], line[MAXN][MAXN], girl[MAXN];
55 int N, T;
56 int n, m;
57 bool dfs(int i){
58     for(int j = 1; j <= m; j++){
59         if(line[i][j] && !used[j]){
60             used[j] = 1;
61             if(girl[j] == 0 || dfs(girl[j])){
62                 girl[j] = i;
63                 return true;
64             }
65         }
66     }
67     return false;
68 }
69 int main() {
70     //freopen("in.txt", "r", stdin);
71     while(~scanf("%d", &n) && n) {
72         scanf("%d%d", &m, &T);
73         memset(line, 0, sizeof(line));
74         while(T--) {
75             int x, y;
76             scanf("%d%d%d", &N, &x, &y);
77             line[x][y] = 1;
78         }
79         int all = 0;
80         memset(girl, 0, sizeof(girl));
81         for(int i = 1; i <= n; i++) {
82             memset(used, 0, sizeof(used));
83             if(dfs(i)) all++;
84         }
85         printf("%d\n", all);
86     }
87 
88     return 0;
89 }
View Code

 

Poj1325 Machine Schedule 最大二分图匹配 匈牙利算法

标签:

原文地址:http://www.cnblogs.com/cshg/p/5783385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!