杭电 hdu 1150 Machine Schedule (二分匹配)
Machine Schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6694 Accepted Submission(s): 3353
Problem 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.
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.
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
Recommend
题意:有很多的工作,而现在有两个机器人,对于每个工作,会给出第一个机器人要用那个模式可以做,第二个机器人要用哪个模式去做
但是换机器人,或者同个机器人换模式,都是要浪费一次时间,希望怎么的选择可以使浪费的时间最少
首先给出n,m,T
n表示第一个机器人有多少模式
m表示第二个机器人有多少模式
T表示有多少工作
接下来是T行,分别a,b,c三个数字
a表示第几个工作
b表示可以用第一个机器人的b模式解决
c表示可以用第二个机器人的c模式解决
等输出只有一个0的时候表示结束
题解:其实就是不用去管它第一个工作之类的,只要看两个机器人的模式就可以了,在一个机器人的一种模式很多的时候,就选择这个模式,
舍弃多重复杂的模式,这样就能抱住转换最少。
这就是一种二分匹配,通过选择多种相同的模式当作1,只能匹配一次,从而找最大匹配数就是答案。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
int n, m;
bool map[105][105]; //用来记录两个机器人直接模式的关系的矩阵,0表示没有关系,1表示连接
bool vis[1005];
int pri[1005]; //用来记录下标下的数字,最终和谁连接
bool find (int x)
{
int i;
for (i = 1; i <= m; i++)
{
if (map[x][i] && !vis[i]) //选择有连接关系的,同时第一次或者是之后用来记录i这个点被暂时标记
{
vis[i] = true;
if (pri[i] == -1 || find(pri[i])) //先看是否点被选择走了,如果选择走了,就看看选走的那个人还有其它可选择的点
{
pri[i] = x;
return true;
}
}
}
return false;
}
int main ()
{
int k, a, b, c, i;
while (scanf ("%d", &n) != EOF)
{
if (n == 0)
return 0;
scanf ("%d%d", &m, &k);
memset (map, false, sizeof (map));
memset (pri, -1, sizeof (pri));
for (i = 0; i < k; i++)
{
scanf ("%d%d%d", &a, &b, &c);
map[b][c] = true; //只记录一二两个机器人的模式就可以了
}
int ans = 0;
for (i = 1; i <= n; i++)
{
memset (vis, false, sizeof (vis));
if (find (i))
ans++;
}
printf ("%d\n", ans);
}
return 0;
}