Machine Schedule
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 14429   Accepted: 6153

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

思路:因为两个机器一开始工作在0号模式,所以(u,0)、(0,v)不需要填边。
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN=;
vector<int> arc[MAXN];
int n,m,k;
int match[MAXN],vis[MAXN];
bool dfs(int u)
{
for(int i=;i<arc[u].size();i++)
{
int to=arc[u][i];
if(!vis[to])
{
vis[to]=;
int w=match[to];
if(w==-||dfs(w))
{
match[to]=u;
match[u]=to;
return true;
}
}
}
return false;
}
int max_flow()
{
int ans=;
memset(match,-,sizeof(match));
for(int i=;i<n;i++)
{
if(match[i]==-)
{
memset(vis,,sizeof(vis));
if(dfs(i))
{
ans++;
}
}
}
return ans;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n!=)
{
scanf("%d%d",&m,&k);
for(int i=;i<MAXN;i++) arc[i].clear();
for(int i=;i<k;i++)
{
int u,v;
scanf("%*d%d%d",&u,&v);
if(u==||v==) continue;
v+=n;
arc[u].push_back(v);
arc[v].push_back(u);
}
int res=max_flow();
printf("%d\n",res);
}
return ;
}

POJ1325(最小顶点覆盖)的更多相关文章

  1. POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题

    在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水, 木板可以重叠,但是所有的草地都不能被木板覆盖. 问至少需要的木板数. 这类题的建图方法: 把矩阵作为一个二分图,以 ...

  2. BZOJ 3140 消毒(最小顶点覆盖)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3140 题意:最近在生物实验室工作的小T遇到了大麻烦. 由于实验室最近升级的缘故,他的分格 ...

  3. poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

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

    Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP

    分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...

  6. hdu1054(最小顶点覆盖)

    传送门:Strategic Game 题意:用尽量少的顶点来覆盖所有的边. 分析:最小顶点覆盖裸题,最小顶点覆盖=最大匹配数(双向图)/2. #include <cstdio> #incl ...

  7. hdu 1150 Machine Schedule(最小顶点覆盖)

    pid=1150">Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  8. poj2594最小顶点覆盖+传递闭包

    传递闭包最开始是在Floyd-Warshall算法里面出现的,当时这算法用的很少就被我忽视了.. 传递闭包是指如果i能到达k,并且k能到达j,那么i就能到达j Have you ever read a ...

  9. hdu1151有向图的最小顶点覆盖

    有向图的最小路径覆盖=V-二分图最大匹配. Consider a town where all the streets are one-way and each street leads from o ...

随机推荐

  1. skynet 创建存储过程脚本

    最近主程更改了数据库的操作方案,由之前的拼写sql脚本转为在mysql端创建好存储过程后,直接调用存储过程. 首先对一个表测试上述过程: 数据库端存储过程:(测试表) CREATE TABLE `ra ...

  2. Search for a range, 在一个可能有重复元素的有序序列里找到指定元素的起始和结束位置

    问题描述:给定一个有序序列,找到指定元素的起始和结束位置.例如:1234555,5,起始4结束6 算法分析:其实就是一个二分查找的利用.但是特殊就在不是找到某个元素,而是找到下标.也就是在nums[m ...

  3. Search In Rotated SortedArray2, 有重复数据的反转序列。例如13111.

    问题描述:反转序列,但是有重复的元素,例如序列13111. 算法思路:如果元素有重复,那么left-mid,就不一定是有序的了,所以不能利用二分搜索,二分搜索必须是局部有序.针对有序序列的反转,如果有 ...

  4. 常见linux系统中RPM包的通用命名规则

    本文重点说一下在常见的linux系统中,RPM包通用的命名规则. RPM包的一般格式为:name-version-arch.rpmname-version-arch.src.rpm 例:httpd-2 ...

  5. CSS 技巧总结

    CSS 技巧和经验列表 1. 如何清除图片下方出现的几像素的空白 方法一: img{display:block;} 方法二: img{vertical-align:top;} 除了top值,还可以设置 ...

  6. ImageView显示图像控件

    ImageView显示图像控件 一.简介 1. 2. ImageView,图像视图,直接继承自View类,它的主要功能是用于显示图片,实际上它不仅仅可以用来显示图片,任何Drawable对象都可以使用 ...

  7. Ajax-07 基于Ajax实现跨域请求

    跨域 跨域名访问,如c1.com域名向c2.com域名发送请求 本质:浏览器存在同源策略机制,同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性. django服务端准备 url. ...

  8. Docker 应用实例

    Docker安装Nginx 方法一.通过 Dockerfile构建 创建Dockerfile 首先,创建目录nginx,用于存放后面的相关东西. runoob@runoob:~$ mkdir -p ~ ...

  9. 【Python】内置函数清单

    Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些函数,不需要定义.最常见的内置函数是: print("Hello Wor ...

  10. Linux安全运维笔记2018-03-01更新

    本人wechat:YWNlODAyMzU5MTEzMTQ=. *** # 修改关键目录文件的权限 chmod u-x,g-r /home/lema chmod 444 /home/lema # 用户权 ...