UVA 2039 Pets(网络流)
Problem Description
Are you interested in pets? There is a very famous pets shop in the center of the ACM city. There are totally m pets in the shop, numbered from 1 to m. One day, there are n customers in the shop, which are numbered from 1 to n. In order to sell pets to as more customers as possible, each customer is just allowed to buy at most one pet. Now, your task is to help the manager to sell as more pets as possible. Every customer would not buy the pets he/she is not interested in it, and every customer would like to buy one pet that he/she is interested in if possible.
Input
There is a single integer T in the first line of the test data indicating that there are T(T≤100) test cases. In the first line of each test case, there are three numbers n, m(0≤n,m≤100) and e(0≤e≤n*m). Here, n and m represent the number of customers and the number of pets respectively.
In the following e lines of each test case, there are two integers x(1≤x≤n), y(1≤y≤m) indicating that customer x is not interested in pet y, such that x would not buy y.
Output
For each test case, print a line containing the test case number (beginning with 1) and the maximum number of pets that can be sold out.
Sample Input
Sample Output
Source
2011年全国大学生程序设计邀请赛(福州)
题意:n只顾客,m个宠物,e种条件,条件表示顾客x不会买宠物y,每个顾客只买一只宠物。求最多卖出几只宠物
思路:网络流,
代码:
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; #define INF 0x3f3f3f3f
const int N = 205;
int T, n, m, e, s, t;
int g[N][N], f[N][N], p[N], a[N]; void init() {
memset(g, 0, sizeof(g));
memset(f, 0, sizeof(f));
scanf("%d%d%d", &n, &m, &e);
s = 0; t = n + m + 1;
for (int i = 1; i <= n; i ++)
g[s][i] = 1;
for (int i = n + 1; i <= n + m; i ++)
g[i][t] = 1;
for (int i = 1; i <= n; i ++)
for (int j = n + 1; j <= n + m; j ++)
g[i][j] = 1;
int u, v;
for (int i = 0; i < e; i ++) {
scanf("%d%d", &u, &v);
g[u][v + n] = 0;
}
} int solve() {
queue<int>q;
int F = 0;
while (1) {
memset(a, 0, sizeof(a));
a[s] = INF;
q.push(s);
while (!q.empty()) {
int u = q.front(); q.pop();
for (int v = 1; v <= t; v ++) {
if (!a[v] && g[u][v] - f[u][v] > 0) {
a[v] = min(a[u], g[u][v] - f[u][v]);
q.push(v); p[v] = u;
}
}
}
if (a[t] == 0) break;
for (int v = t; v; v = p[v]) {
f[p[v]][v] += a[t];
f[v][p[v]] -= a[t];
}
F += a[t];
}
return F;
}
int main() {
int cas = 0;
scanf("%d", &T);
while (T--) {
init();
printf("Case %d: %d\n", ++cas, solve());
}
return 0;
}
UVA 2039 Pets(网络流)的更多相关文章
- FZU - 2039 Pets (二分图匹配 2011年全国大学生程序设计邀请赛(福州))
Description Are you interested in pets? There is a very famous pets shop in the center of the ACM ci ...
- fzu 2039 Pets (简单二分图 + (最大流 || 二分图))
Are you interested in pets? There is a very famous pets shop in the center of the ACM city. There ar ...
- 紫书 例题11-8 UVa 11082(网络流最大流)
这道题的建模真的非常的秀, 非常牛逼. 先讲建模过程.源点到每一行连一条弧, 容量为这一行的和减去列数, 然后每一列到汇点连一条弧, 容量为这一列 的和减去行数, 然后每一行和列之间连一条弧, 容量为 ...
- uva 563 - Crimewave 网络流
题目链接 有一个n*m的图, 里面有q个人, 每个点只能走一次, 问这q个人是否都能够走出这个图. 对于每个人, 建边(s, u, 1), 对于每个边界的格子, 建边(u', t, 1), 对于其他格 ...
- A Plug for UNIX UVA - 753(网络流)
题意:n个插座,m个设备及其插头类型,k种转换器,没有转换器的情况下插头只能插到类型名称相同的插座中,问最少剩几个不匹配的设备 lrj紫书里面讲得挺好的. 先跑一遍floyd,看看插头类型a能否转换为 ...
- 紫书 习题 11-4 UVa 1660 (网络流拆点法)
这道题改了两天-- 因为这道题和节点有关, 所以就用拆点法解决节点的容量问题. 节点拆成两个点, 连一条弧容量为1, 表示只能经过一次. 然后图中的弧容量无限. 然后求最小割, 即最大流, 即为答案. ...
- 紫书 例题11-7 UVa 753 (网络流最大流)
设一个源点, 到所有设备连一条弧, 容量为1, 然后设一个汇点, 所有插座到汇点连弧, 容量为1, 然后 转换器也连一条弧, 容量为1. 最后最大流就是答案.其中注意节点数要开大一些. #includ ...
- UVA 10480 Sabotage (网络流,最大流,最小割)
UVA 10480 Sabotage (网络流,最大流,最小割) Description The regime of a small but wealthy dictatorship has been ...
- POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for UNIX / UVAlive 5418 A Plug for UNIX / SCU 1671 A Plug for UNIX (网络流)
POJ 1087 A Plug for UNIX / HDU 1526 A Plug for UNIX / ZOJ 1157 A Plug for UNIX / UVA 753 A Plug for ...
随机推荐
- BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
题目 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 Time Limit: 5 Sec Memory Limit: 64 MB Description ...
- (3)选择元素——(2)文档对象模型(The Document Object Model)
One of the most powerful aspects of jQuery is its ability to make selecting elements in the DOM easy ...
- win32多线程程序设计笔记(第四章下)
上一笔记讲了同步机制中的临界区域(Critical Sections).互斥器(Mutexes),下面介绍同步机制中的另外两种. 信号量(Semaphores) 举个例子: 现在有人要租车,接待他的代 ...
- C#_会员管理系统:开发一(用户登录)
首先创建数据库: [Vip] 创建三张表: 分别是: [VipInformation](会员信息) [Log](日志) [VipAccount](账户权限) 详细语句: --创建数据库[Vip] cr ...
- Ajax以及类似百度搜索框的demo
public class Ajax01 extends HttpServlet{ @Override protected void service(HttpServletRequest request ...
- jQuery特效手风琴特效 手写手风琴网页特效
今天写一个简单的手风琴效果,不用插件,利用强大的jQuery,写一个手风琴效果. 页面预览,请点击这里预览: 手风琴预览 案例分析: html结构 就是一个大盒子里面放着5个li,每个li的小小是2 ...
- redis(三)redis+Keepalived主从热备秒级切换
一 简介 安装使用centos 5.10 Master 192.168.235.135 Slave 192.168.235.152 Vip 192.168.235.200 编译环境 yum -y in ...
- 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...
- 进入MFC讲坛的前言(五)
框窗.视图和文档及其关系 MFC架构的另外一个特色是它的框窗.视图和文档这个三位一体的结构,它是一个典型的MVC(Model.View and Controler)结构.严格的讲,框窗不属于MVC中的 ...
- Mybatis3 框架理解
最近工作比较闲,维护一个政府机关的短信发送平台,大部分业务逻辑都在Oracle数据库上,但自己明明应聘的是Java开发啊!!!整天写存储过程的我还是有一颗写高级语言的心啊!!!好吧!!!先找个数据库方 ...