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

12 2 21 22 1

 Sample Output

Case 1: 2

 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(网络流)的更多相关文章

  1. FZU - 2039 Pets (二分图匹配 2011年全国大学生程序设计邀请赛(福州))

    Description Are you interested in pets? There is a very famous pets shop in the center of the ACM ci ...

  2. fzu 2039 Pets (简单二分图 + (最大流 || 二分图))

    Are you interested in pets? There is a very famous pets shop in the center of the ACM city. There ar ...

  3. 紫书 例题11-8 UVa 11082(网络流最大流)

    这道题的建模真的非常的秀, 非常牛逼. 先讲建模过程.源点到每一行连一条弧, 容量为这一行的和减去列数, 然后每一列到汇点连一条弧, 容量为这一列 的和减去行数, 然后每一行和列之间连一条弧, 容量为 ...

  4. uva 563 - Crimewave 网络流

    题目链接 有一个n*m的图, 里面有q个人, 每个点只能走一次, 问这q个人是否都能够走出这个图. 对于每个人, 建边(s, u, 1), 对于每个边界的格子, 建边(u', t, 1), 对于其他格 ...

  5. A Plug for UNIX UVA - 753(网络流)

    题意:n个插座,m个设备及其插头类型,k种转换器,没有转换器的情况下插头只能插到类型名称相同的插座中,问最少剩几个不匹配的设备 lrj紫书里面讲得挺好的. 先跑一遍floyd,看看插头类型a能否转换为 ...

  6. 紫书 习题 11-4 UVa 1660 (网络流拆点法)

    这道题改了两天-- 因为这道题和节点有关, 所以就用拆点法解决节点的容量问题. 节点拆成两个点, 连一条弧容量为1, 表示只能经过一次. 然后图中的弧容量无限. 然后求最小割, 即最大流, 即为答案. ...

  7. 紫书 例题11-7 UVa 753 (网络流最大流)

    设一个源点, 到所有设备连一条弧, 容量为1, 然后设一个汇点, 所有插座到汇点连弧, 容量为1, 然后 转换器也连一条弧, 容量为1. 最后最大流就是答案.其中注意节点数要开大一些. #includ ...

  8. UVA 10480 Sabotage (网络流,最大流,最小割)

    UVA 10480 Sabotage (网络流,最大流,最小割) Description The regime of a small but wealthy dictatorship has been ...

  9. 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 ...

随机推荐

  1. 16个值得个人站长做的广告联盟[转自cnzz]

    建站也有好多年了,也建了几个站,有些微波收入, 反复测试了挺多广告联盟, 下面介绍一下: 1.googleadsense联盟: 推荐指数:☆☆☆☆☆ Google广告联盟是现在信誉最好的广告提供商之一 ...

  2. 奔小康赚大钱(km)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  3. Linux下which、whereis、locate、find 区别

    我们经常在linux要查找某个文件或命令,但不知道放在哪里了,可以使用下面的一些命令来搜索.which      查看可执行文件的位置 whereis    查看文件的位置 locate     配合 ...

  4. MFC下的日历表

    // CalenderDlg.h : header file // #if !defined(AFX_CALENDERDLG_H__8DC8F113_2A47_45B8_8266_75CB406D68 ...

  5. 【Hibernate】HQL new map

    HQL 查询如果需要带出对象(比如:OneToOne子对象)的一两个属性,而不是全部带出.可以使用 select new map的方法带出. 1.lazy还是设置为false /**关联属性对象*/ ...

  6. ARM异常---一个DataAbort的触发过程:

    一个DataAbort异常的触发过程://////////////////////////////xxxx.inc_STACK_BASEADDRESS EQU 0x33ff8000_MMUTT_STA ...

  7. BZOJ 2242: [SDOI2011]计算器( 快速幂 + 扩展欧几里德 + BSGS )

    没什么好说的... --------------------------------------------------------------------- #include<cstdio&g ...

  8. 设计模式 ( 十七) 状态模式State(对象行为型)

    设计模式 ( 十七) 状态模式State(对象行为型) 1.概述 在软件开发过程中,应用程序可能会根据不同的情况作出不同的处理.最直接的解决方案是将这些所有可能发生的情况全都考虑到.然后使用if... ...

  9. HTMLParser-简单HTML和XHTML解析

    使用HTMLParser模块解析HTML页面 HTMLParser是python用来解析html和xhtml文件格式的模块.它可以分析出html里面的标签.数据等等,是一种处理html的简便途径.HT ...

  10. 西安力邦智能医疗&amp;可穿戴设备沙龙--第1期---苹果HealthKit、谷歌GoogleFit来袭,智能医疗要爆发吗?

    背    景: "可穿戴设备"成为2014的行业热点,从Google Glass到苹果iWatch, 越来越多的企业推出了包含眼镜.腕带.鞋等各种可穿戴设备,"可穿戴&q ...