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

Beijing 2002

【题解】

每个任务要么用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\n", xiongyali());
61     }
62     return 0;
63 }

POJ1325

时间: 2024-08-01 10:42:33

POJ1325 Machine Schedule的相关文章

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

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 an

POJ-1325 Machine Schedule 二分图匹配 最小点覆盖问题

POJ-1325 题意: 有两台机器A,B,分别有n,m种模式,初始都在0模式,现在有k项任务,每项任务要求A或者B调到对应的模式才能完成.问最少要给机器A,B调多少次模式可以完成任务. 思路: 相当于是在以n.m个点构成的二分图中,求二分图的最小顶点覆盖数(就是每个任务都涉及到,所需的顶点数).根据Konig定理,二分图的最小顶点覆盖数就是求最大匹配数,注意这里是Base 0的,就是初始不用调整模式就可以完成0模式的任务,所以读入的时候不用考虑与0相连的边. #include <algorit

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

题目链接: http://poj.org/problem?id=1325 题目大意: 有两台机器A和B,机器A有N种不同的模式,编号为0~N-1.机器B有M种不同的模式,编号为0~M-1. 在一开始的时候,机器A和B都处于0模式.现在需要用两台机器来处理K项任务,任务编号为0~K-1.每 一项任务都可以在A或B的指定状态下完成.例如任务1可以在机器A的2模式下完成,也可以在机器B的4 模式下完成.对于第i想任务用(i,x,y)来表示第i项任务可以在机器A的x模式下或机器B的y模式下完成. 为了完

hdu1150——Machine Schedule

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5965    Accepted Submission(s): 2999 Problem Description As we all know, machine scheduling is a very classical problem in compu

HDU 1150:Machine Schedule(二分匹配,匈牙利算法)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5371    Accepted Submission(s): 2658 Problem Description As we all know, machine scheduling is a very classical problem in compu

HDU 1150 Machine Schedule (二分匹配)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) 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

hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6701    Accepted Submission(s): 3358 Problem Description As we all know, machine scheduling is a very classical problem in comput

POJ 1325 Machine Schedule (二分图最小点集覆盖 匈牙利算法)

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