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

1 2 2 2 1 2 2 1

Sample Output

Case 1: 2

题目大意:有n个顾客。有m仅仅宠物,而且顾客有e个要求。要求内容为。第i号顾客不想买第j号宠物。问最多能卖多少仅仅宠物。

解题思路:能够用最大流。能够用匈牙利hungary算法来求二分图。

最大流的时候。要注意拆点。建立一个超级源点连接全部的顾客,容量为INF。建立一个超级汇点使全部宠物连向他,容量为INF。

顾客和宠物各自拆成两个点,容量为1,这样能够保证,每一个顾客仅仅能买一仅仅宠物,每仅仅宠物仅仅能被一个顾客购买。然后依据e个要求,建立顾客和宠物之间的边,容量为1,之后求最大流。这种方法更复杂更耗时。所以这题最好用匈牙利算法。

最大流

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 1005;
const int OF1 = 100;
const int OF2 = 200;
const int FIN = 505;
const int INF = 0x3f3f3f3f;
int n, m, e, f[N][N], s, t;
struct Edge{
int from, to, cap, flow;
}; vector<Edge> edges;
vector<int> G[N]; void init() {
s = 0, t = FIN;
for (int i = 0; i < N; i++) G[i].clear();
edges.clear();
memset(f, 0, sizeof(f));
} void addEdge(int from, int to, int cap, int flow) {
edges.push_back((Edge){from, to, cap, 0});
edges.push_back((Edge){to, from, 0, 0});
int temp = edges.size();
G[from].push_back(temp - 2);
G[to].push_back(temp - 1);
}
void input() {
int a, b;
for (int i = 0; i < e; i++) {
scanf("%d %d", &a, &b);
f[a][b] = 1;
}
for (int i = 1; i <= n; i++) {
addEdge(0, i, INF, 0);
addEdge(i, i + OF1, 1, 0);
}
for (int i = 1; i <= m; i++) {
addEdge(i + OF2, i + OF2 + OF1, 1, 0);
addEdge(i + OF2 + OF1, FIN, INF, 0);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (!f[i][j]) {
addEdge(i + OF1, j + OF2, 1, 0);
}
}
}
}
int vis[N], d[N];
int BFS() {
memset(vis, 0, sizeof(vis));
// for (int i = 0; i < FIN; i++) d[N] = INF;
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (int i = 0; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[u] + 1;
Q.push(e.to);
}
}
}
return vis[t];
} int cur[N];
int DFS(int u, int a) {
if (u == t || a == 0) return a;
int flow = 0, f;
for (int &i = cur[u]; i < G[u].size(); i++) {
Edge &e = edges[G[u][i]];
if (d[u] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[u][i]^1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
int MF() {
int ans = 0;
while (BFS()) {
memset(cur, 0, sizeof(cur));
ans += DFS(s, INF);
}
return ans;
}
int main() {
int T, Case = 1;
scanf("%d", &T);
while (T--) {
printf("Case %d: ", Case++);
scanf("%d %d %d", &n, &m, &e);
init();
input();
int ans = MF();
printf("%d\n", ans);
}
return 0;
}

匈牙利算法

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef __int64 ll;
const int N = 505;
int n, m, e, ans;
int G[N][N], vis[N], R[N];
void input() {
memset(G, 1, sizeof(G));
memset(R, 0, sizeof(R));
int a, b;
for (int i = 0; i < e; i++) {
scanf("%d %d", &a, &b);
G[a][b] = 0;
}
}
int find(int x) {
for (int i = 1; i <= m; i++) {
if (G[x][i] && !vis[i]) {
vis[i] = 1;
if (R[i] == 0 || find(R[i])) {
R[i] = x;
return 1;
}
}
}
return 0;
}
void hungary() {
for (int i = 1; i <= n; i++) {
memset(vis, 0, sizeof(vis));
if (find(i)) ans++;
}
}
int main() {
int T, Case = 1;
scanf("%d", &T);
while (T--) {
printf("Case %d: ", Case++);
ans = 0;
scanf("%d %d %d", &n, &m, &e);
input();
hungary();
printf("%d\n", ans);
}
return 0;
}

fzu 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. 【bzoj3291】Alice与能源计划 模拟费用流+二分图最大匹配

    题目描述 在梦境中,Alice来到了火星.不知为何,转眼间Alice被任命为火星能源部长,并立刻面临着一个严峻的考验. 为了方便,我们可以将火星抽象成平面,并建立平面直角坐标系.火星上一共有N个居民点 ...

  3. 利用JS实现简单的瀑布流效果

    哈哈, 我又来啦, 在这一段时间里, 我简单的学习了一下javascript(JS), 虽然不是很懂啦, 但是我也简单的尝试着做了点小东西, 就比如现在流行的瀑布流效果, 经过我的努力终于成功的完成了 ...

  4. POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)

    POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...

  5. 经典网络流题目模板(P3376 + P2756 + P3381 : 最大流 + 二分图匹配 + 最小费用最大流)

    题目来源 P3376 [模板]网络最大流 P2756 飞行员配对方案问题 P3381 [模板]最小费用最大流 最大流 最大流问题是网络流的经典类型之一,用处广泛,个人认为网络流问题最具特点的操作就是建 ...

  6. POJ2195 Going Home[费用流|二分图最大权匹配]

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22088   Accepted: 11155 Desc ...

  7. POJ2195 Going Home (最小费最大流||二分图最大权匹配) 2017-02-12 12:14 131人阅读 评论(0) 收藏

    Going Home Description On a grid map there are n little men and n houses. In each unit time, every l ...

  8. 【BZOJ 3308】 3308: 九月的咖啡店 (费用流|二分图最大权匹配)

    3308: 九月的咖啡店 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 244  Solved: 86 Description 深绘里在九份开了一家咖 ...

  9. hdu 3081(二分+并查集+最大流||二分图匹配)

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. git使用代理clone加速

    不设置代理10kb/s不到....,设置后,500kb/s左右跑- 开shadowsocks,代理127.0.0.1:1080 编写一个脚本 /YOUR PATH/gitproxy.sh #!/bin ...

  2. (转)Kettle命令行

    kettle使用命令行来运行ktr和kjb 1:cmd方式运行 1.ktr的运行:运行transformation文件是通过Pan.bat来运行的. 打开cmd命令行窗口,转到Pan.bat所在的目录 ...

  3. 字符串hash-26进制与10进制互相转换

    Lovekey http://acm.hdu.edu.cn/showproblem.php?pid=2100 #include <bits/stdc++.h> using namespac ...

  4. Ubuntu免安装配置MySQL

    1.下载mysql http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.21-linux-glibc2.5-x86_64.tar.gz 2.解压 ta ...

  5. AutoCAD如何将dwf转成dwg格式

    dwf转成dwg怎么转, 悬赏分:30 - 解决时间:2009-11-22 10:19 重金:dwf转成dwg怎么转, 我是用在出图上的. 最佳答案 Design Web Format (DWF) 文 ...

  6. DFRobot万物互联大赛第二轮

    前言 最近放在阳台的花草被啥东西给吃了,然后厨房挂在墙上的小虾米也不知道咋的被抓破吃光了(我怀疑是隔隔壁两条泰迪),所以打算做个简单的项目,教训一下偷吃贼.时间比较仓促,内容比较多,能力有比较有限,好 ...

  7. 【VBS】使用Visual Studio调试VBS程序

    首先要确保机器上安装了Visual Stuido, 然后打开命令行窗口执行如下命令,会弹出是否使用Visual Studio进行调试的确认窗口. 点[是]进行调试. WScript.exe [vbs文 ...

  8. Java Transaction Management

    Just a few weeks ago, I had a discussion with one of my colleagues about how to manage the transacti ...

  9. HDU1272 小希的迷宫(基础并查集)

    杭电的图论题目列表.共计500题,努力刷吧 AC 64ms #include <iostream> #include <cstdlib> #include <cstdio ...

  10. 2、C++ 的升级

    1.内联函数     define 可以定义宏代码片段,但是,C++ 推荐使用内联函数替代宏代码片段. inline int f(int a, int b) { }     只需要在 函数定义(实现) ...