Machine Schedule
Time Limit: 1000MS | | Memory Limit: 10000K |
Total Submissions: 17649 | | Accepted: 7385 |
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机器有n个模式,B机器有m个模式,一共有k个任务,初始的时候A、B机器都是模式0,一个任务可以在机器A的ai模式下完成,也可以在机器B的bi模式下完成,每一次机器想要更换模式都需要重新启动,问完成所有任务最少重新启动多少次?
分析:
最小点覆盖是指假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖所有的边。
最小点覆盖=最大匹配数;证明:http://www.matrix67.com/blog/archives/116
//二分图最大匹配模板,二分图都是无向图
//调用下面算法前,保证本图是二分图
/*************vecotr模板*****************/
#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
const int maxn = 510;
struct Max_Match
{
int n,m;//左右点集大小,点从1开始编号
vector<int> g[maxn];//g[i]表示左边第i个点邻接的右边点的集合
bool vis[maxn];//vis[i]表示右边第i个点是否在本次match中被访问过
int left[maxn];//left[i]==j表右边第i个点与左边第j个点匹配,为-1表无点匹配
void init(int n,int m)
{
this->n = n;
this->m = m;
for (int i = 0; i <= n; i++) g[i].clear();
memset(left, -1, sizeof(left));
}
//判断从左u点是否可以找到一条增广路
bool match(int u)
{
for (int i = 0; i<g[u].size(); i++)
{
int v = g[u][i];
if (!vis[v])
{
vis[v] = true;
if (left[v] == -1 || match(left[v]))//找到增广路
{
left[v] = u;
return true;
}
}
}
return false;
}
//返回当前二分图的最大匹配数
int solve()
{
int ans = 0;//最大匹配数
for (int i = 1; i <= n; i++)//每个左边的节点找一次增广路
{
memset(vis, 0, sizeof(vis));
if (match(i)) ans++;//找到一条增广路,形成一个新匹配
}
return ans;
}
}MM;
/*************vecotr模板*****************/
int main()
{
int n, m, k;
while (scanf("%d%d%d", &n,&m,&k) !=-1 && n)
{
MM.init(n-1 , m -1);
while (k--)
{
int i, u, v;
scanf("%d%d%d", &i, &u, &v);
if (u == 0 || v == 0) continue;
MM.g[u].push_back(v);
}
printf("%d\n", MM.solve());
}
return 0;
}