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
题解:最小点覆盖数==最大匹配边数
参考代码:
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<vector>
#include<map>
using namespace std;
const int maxn=;
int n,m,k,ok[maxn];
bool a[maxn][maxn],vis[maxn]; bool Find(int x)
{
for(int i=;i<=m;i++)
{
if(a[x][i]&&!vis[i])
{
vis[i]=true;
if(!ok[i]||Find(ok[i]))
{
ok[i]=x;
return true;
}
}
}
return false;
}
int maxP()
{
int ans=;
memset(ok,,sizeof(ok));
for(int i=; i<=n; i++)
{
memset(vis,false,sizeof(vis));
if(Find(i)) ans++;
}
return ans;
}
int main()
{
int kase=;
while(scanf("%d",&n) && n)
{
scanf("%d%d",&m,&k);
memset(a,false,sizeof(a));
int x,y,z;
while(k--)
{
cin>>x>>y>>z;
a[y][z]=true;
}
int ans=maxP();
int cnt=;
printf("%d\n",ans);
}
return ;
}

最新文章

  1. java中的锁
  2. Jquery 操作CheckBox ,RadioButtonList,DropDownList
  3. HQL查询——select子句
  4. kudu playground
  5. RandomUser – 生成随机用户 JSON 数据的 API
  6. find command in linux terminal
  7. js中的 !!
  8. ZOJ 1202 Divide and Count(排列组合)
  9. iOS8 Core Image In Swift:人脸检测以及马赛克
  10. Xcode-App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.
  11. JavaScript中的栈及通过栈操作的实例
  12. React Router 使用教程
  13. 使用rsync备份与同步文件
  14. frame的用法
  15. C++模板的要点
  16. 前端入门3-CSS基础
  17. Window应急响应(四):挖矿病毒
  18. Spring Boot 揭秘与实战 源码分析 - 工作原理剖析
  19. C++ STL 数据结构与算法 —— 排序
  20. pyinstaller 将.py生成.exe ----报错 “IndexError: tuple index out of range”

热门文章

  1. K8S入门系列之集群yum安装(一)
  2. Servlet相关学习
  3. 微服务SpringCloud之GateWay熔断、限流、重试
  4. Nginx做缓存服务器
  5. Sturts2整合Spring报错:org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml];
  6. Leetcode算法【114. 二叉树展开为链表】
  7. hdu 5495 LCS (置换群)
  8. mysql 索引笔记
  9. 简单地迁移你的android jni代码逻辑到iOS - 编写iOS下jni.h的替代 - ocni.h
  10. Redux中间件Redux-thunk的配置