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

Machine Schedule

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 }

				
时间: 2024-07-31 14:30:36

Poj1325 Machine Schedule 最大二分图匹配 匈牙利算法的相关文章

USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John r

HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49    Accepted Submission(s): 14 Problem Description There is a kindom of obsession, so people in this kingdom do things very

HDU1507 Uncle Tom&#39;s Inherited Land* 二分图匹配 匈牙利算法 黑白染色

原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1*2的矩形覆盖所有的不废的点,并且不重叠,问最多可以覆盖多少个1*2的矩形,输出方案,有SPJ. 输入描述: 多组数据,每组首先两个数n,m(如果n和m为0,则结束程序) 然后给出k 然后给出k个二元组(x,y)表示废点的坐标. 题解 按照前两片博文的算法已经不行了,因为方案不对了. 所以我们要进行

Machine Schedule(二分图匹配之最小覆盖点,匈牙利算法)

个人心得:二分图啥的一点都不知道,上网借鉴了下,请参考http://blog.csdn.net/thundermrbird/article/details/52231639 加上自己的了解,二分图就是将图形拆分成俩半,俩边的点通过边界相连接.匹配就是不存在俩条边存在同一顶点,就是一个顶点最多连接一条边. 匈牙利算法就是用增广路进行更新,仔细一想确实如此,如果存在这样的途径那么必然可以用亦或就行维护更新,细节看参考. 不过碍于自己图论太low,还是无法运用的好,比如这题,就是最小覆盖点,关于最小覆

POJ1325 Machine Schedule 【二分图最小顶点覆盖】

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11958   Accepted: 5094 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

POJ 1325 二分图匹配/匈牙利算法

Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11922 Accepted: 5077 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 p

矩阵游戏|ZJOI2007|BZOJ1059|codevs1433|luoguP1129|二分图匹配|匈牙利算法|Elena

1059: [ZJOI2007]矩阵游戏 Time Limit: 10 Sec  Memory Limit: 162 MB Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏--矩阵游戏.矩阵游戏在一个N *N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两种操作:行交换操作:选择 矩阵的任意两行,交换这两行(即交换对应格子的颜色)列交换操作:选择矩阵的任意行列,交换这两列(即交换 对应格子的颜色)游戏的目标,即通过若干次操作,

网络流24题 第一题 - 洛谷2756 飞行员配对方案 二分图匹配 匈牙利算法

欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 题意概括 裸的二分图匹配 题解 匈牙利算法 上板子 代码 #include <cstring> #include <cstdio> #include <algorithm> #include <cstdlib> #include <cmath> using namespace std; const int N=100+5; int m,n,a,b,match[N

图论-二分图匹配-匈牙利算法

有关概念: 二分图:图G中的点集可以分为两个互不相交的子集,且G中的每条边连接的两个点分别属于这两个子集 二分图匹配:二分图G的子图M中每个结点上只连一条边,则称M为一个匹配 极大匹配:无法再向二分图中加边且满足匹配条件的匹配 最大匹配:所有极大匹配中边数最多的一个 增广路:若P为图G上连接两个未匹配结点的路径,且已匹配边和未匹配边在P上交替出现,则称P为相对于M的一条增广路 匈牙利算法即用来求二分图的极大匹配 思路: 在图G中找出增广路P,对P上每一条边取反(即已匹配边改为未匹配边,未匹配边改