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 ...
随机推荐
- Uva 11694 Gokigen Naname
基本思路是Dfs: 1. 一个一个格子摆放,以每个各自的左上角的点为基准点代表格子,比如(0,0)代表(0,0)(0,1)(1,0)(1,1)组成的格子,(0,1)代表(0,1)(0,2)(1,1), ...
- Vmware ESX 5.0 安装与部署
近期我公司部署了虚拟化,採购了两台Dell R710的server(CPU:64位双核,主频:2.4GHZ, 32G 内存.硬盘:2块300G做Riad 1.3块2T做Riad 5 .10块网卡),在 ...
- Struts2-ActionContext
官方解释: The ActionContext is the context in which an {@link Action} is executed. Each context is basic ...
- ring0和ring3的区别
现在探讨内核程序和应用程序之间的本质区别.除了能用WDK编写内核程序和阅读一部分Windows的内核代码之外,我们还需要了解它们的本质是什么,它们和我们熟悉的应用程序有什么区别. Intel的x86处 ...
- SpringMVC入门二: 1规范结构, 2简单整合MyBatis
昨天拿springMVC写的helloworld结构不好, 这次先调整一下体系结构 , 然后简单整合一下MyBatis spring的配置还是以注解为主, 不过MyBatis的映射文件什么的还是拿xm ...
- 【每日一摩斯】-Shared Pool优化和Library Cache Latch冲突优化 (1523934.1)-系列3
减轻Shared Pool负载 Parse一次并执行多次 在OLTP类型的应用中,最好的方法是只让一个语句被解析一次,然后保持这个cursor的打开状态,在需要的时候重复执行它.这样做的 ...
- CCIE路由实验(4) -- BGP路由控制
1.过滤BGP路由的方法2.用AS-path filter控制路由3.用Community Filter控制路由 enableconf tno ip do loenable pass ciscolin ...
- objective-C 初识
objective-C objective-c 是c语言的改进版 一.方法的定义: 格式: -/+(返回值类型)方法名:(参数类型) 参数名 [方法名] : (参数类型) 参数名......... 例 ...
- 在 vb中 "end","unload me","exit sub" 之间的区别
之前就想过这个问题,这么熟悉的几个东西居然对他们分析的不是很透彻. “End” 跟 “Unload Me” 在敲程序 的时候经常敲到,“exit sub” 更是熟悉,下面,解析: End ...
- 利用JNI技术在Android中调用C++形式的OpenGL ES 2.0函数
1. 打开Eclipse,File-->New-->Project…-->Android-->AndroidApplication Projec ...