POJ1325(最小顶点覆盖)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 14429 | Accepted: 6153 |
Description
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 will be terminated by a line containing a single zero.
Output
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(最小顶点覆盖)的更多相关文章
- POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题
在一个n*m的草地上,.代表草地,*代表水,现在要用宽度为1,长度不限的木板盖住水, 木板可以重叠,但是所有的草地都不能被木板覆盖. 问至少需要的木板数. 这类题的建图方法: 把矩阵作为一个二分图,以 ...
- BZOJ 3140 消毒(最小顶点覆盖)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3140 题意:最近在生物实验室工作的小T遇到了大麻烦. 由于实验室最近升级的缘故,他的分格 ...
- poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】
Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP
分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...
- hdu1054(最小顶点覆盖)
传送门:Strategic Game 题意:用尽量少的顶点来覆盖所有的边. 分析:最小顶点覆盖裸题,最小顶点覆盖=最大匹配数(双向图)/2. #include <cstdio> #incl ...
- hdu 1150 Machine Schedule(最小顶点覆盖)
pid=1150">Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- poj2594最小顶点覆盖+传递闭包
传递闭包最开始是在Floyd-Warshall算法里面出现的,当时这算法用的很少就被我忽视了.. 传递闭包是指如果i能到达k,并且k能到达j,那么i就能到达j Have you ever read a ...
- hdu1151有向图的最小顶点覆盖
有向图的最小路径覆盖=V-二分图最大匹配. Consider a town where all the streets are one-way and each street leads from o ...
随机推荐
- 线段树 - HDU1166 - 敌兵布阵
2017-07-29 16:41:00 writer:pprp 线段树跟区间操作相关,想要在题目限定的时间内解决问题就需要用线段树这种数据结构来解决: 线段树是一种二叉平衡树 参考书目:张新华的< ...
- 【转】和菜鸟一起学linux之DBUS基础学习记录
[原文] D-Bus三层架构 D-Bus是一个为应用程序间通信的消息总线系统, 用于进程之间的通信.它是个3层架构的IPC 系统,包括: 1.函数库libdbus ,用于两个应用程序互相联系和交互消息 ...
- Java对象初始化
自动初始化(默认值) 一个类的所有基本数据成员都会得到初始化,运行下面的例子可以查看这些默认值: class Default{ boolean t; char c; byte b; short s; ...
- BZOJ1484 [HNOI2009]通往城堡之路
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- JSP web.xml <jsp-config>标签使用详解
<jsp-config> 包括 <taglib> 和 <jsp-property-group> 两个子元素.其中<taglib> 元素在JSP 1.2 ...
- lucene学习-1 - 准备工具
需要准备的内容: jdk 1.7 eclipse Kepler lucene 4.10.2 luke 4.10.2 以及一些txt文档 其他的不需多说,只简要介绍下luke. 以下内容来自百度百科: ...
- uva-1449-AC自动机
题目链接https://vjudge.net/problem/UVA-1449 题目大意:给出N(N<150)个长度不超过L(70)的匹配串和一个长度小于1e6的文本串,在文本串中找出出现次数最 ...
- 【spark】示例:求极值
我们有这样的数据 1.建立SparkContext读取数据 (1)建立sc (2)通过sc.textFile()读取数据创建Rdd 2.过滤数据 通过filter(line => line.tr ...
- 【Wannafly挑战赛10 - B】小H和密码(DP)
试题链接:https://www.nowcoder.com/acm/contest/72/B 题目描述 小H在击败怪兽后,被一个密码锁挡住了去路 密码锁由N个转盘组成,编号为1~N,每 ...
- Context的作用
context用于访问全局资源 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSta ...