Machine Schedule
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.
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
题目链接:HDU-1150
题目大意:有两台机器加工零件,A有n种工作模式(a0,a1,a2,a3…an),B有m种工作模式(b0,b1,b2…bm)。现在需要加工k个零件,每个零件输入(i,x,y)表示第i这个零件在A机器上需要用x模式,在B机器上需要y模式完成。零件加工顺序任意,每次改变机器模式需要花费1点值。问最小花费为多少,即最少改变几次模式?
注意,两台机器初始状态都在0状态
题目思路:
1.每个任务必须完成,即必须选择一台机器完成。
2.将A,B两台机器分为两组端点,每个任务只能用A机器完成或者B机器完成,选了一台就不能选另外一台。
3.连线,如图所示。
4.每条线代表一个任务,我们要完成所有任务就是要用最小的点覆盖所有的边,所以问题就是求最小点覆盖
5.我们又知道,最小点覆盖=二分图最大匹配,所以问题转化为二分图最大匹配。
以下是代码:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#include <deque>
#include <list>
using namespace std;
/*数组下标从1开始*/
const int N = 300;
char map[N][N];
int col[N][N],row[N][N];
int link[N],head[N];
bool vis[N];
int cnt,n,m;
int R,C; //两种物品的数量
struct Edge
{
int to;
int next;
};
Edge edge[N*N];
void Init() //初始化
{
cnt = 0;
memset(head,-1,sizeof(head));
memset(col,0,sizeof(col));
memset(row,0,sizeof(row));
}
void add(int u,int v) //u和v相连
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
bool dfs(int u)
{
for(int i=head[u]; ~i; i=edge[i].next)
{
int v = edge[i].to;
if(!vis[v])
{
vis[v] = 1;
if(link[v] == -1 || dfs(link[v]))
{
link[v] = u;
return true;
}
}
}
return false;
}
int match() //直接调用
{
int ans = 0;
memset(link,-1,sizeof(link));
for(int i=1; i<=R; i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i)) ans++;
}
return ans;
}
int main()
{
int k;
while(cin >> R && R)
{
cin >> C >> k;
Init(); //初始化
for (int i = 0; i < k; i++)
{
int u,v,no;
cin >> no >> u >> v;
if (u == 0 && v == 0) continue;
add(u,v);
}
int ans = match();
cout << ans << endl;
}
return 0;
}