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 ...
随机推荐
- 孙弘与Masa Maso 做互联网最贵的衬衫(2)_人物对话_中国时尚品牌网
孙弘与Masa Maso 做互联网最贵的衬衫(2)_人物对话_中国时尚品牌网 孙弘与Masa Maso 做互联网最贵的衬衫(2)
- HDU 4569Special equations2012长沙邀请赛E题(数学知识)
Special equations Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 第一种:NStread
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- HNU13028Attacking rooks (二分匹配,一行变多行,一列变多列)
Attacking rooks Time Limit: 20000ms, Special Time Limit:50000ms, Memory Limit:65536KB Total submit u ...
- 浅谈独立使用NDK编译库文件(Android)
阅读前准备 这是一篇相对入门的文章.文中会涉及到少许NDK的知识,但个人认为对初学者来说都相对比较实用,因为都是在平时项目中遇到的(目前自己也是初学者).一些其他高深的技术不再本文探讨范围之内(因为我 ...
- vim下使用YouCompleteMe实现代码提示、补全以及跳转设置
配置YouCompleteMe 1. 安装vundle vundle是一个管理vim插件的工具,使用vundle安装YouCompleteMe比较方便. 按照作者在https://github.com ...
- C#学习之在辅助线程中修改UI控件----invoke方法
Invoke and BeginInvoke 转载地址:http://www.cnblogs.com/worldreason/archive/2008/06/09/1216127.html 在Invo ...
- Uniconnection 连 mysql 有时会断线的
你定义localfailover:=ture.断线后会自己接上 firedac没这种功能.只有unidac有
- linux下java窗口,正确显示中文
Tip1 1.在 JAVA_HOME/jre/lib/fonts/ 下建立个目录 fallback 2.在 fallback 里弄个中文字体最简单ln一下就好了 比如: ln -s /usr/shar ...
- 【引用】Linux 内核驱动--多点触摸接口
本文转载自James<Linux 内核驱动--多点触摸接口> 译自:linux-2.6.31.14\Documentation\input\multi-touch-protocol.t ...