fzu 2039 Pets (简单二分图 + (最大流 || 二分图))
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 (简单二分图 + (最大流 || 二分图))的更多相关文章
- FZU - 2039 Pets (二分图匹配 2011年全国大学生程序设计邀请赛(福州))
Description Are you interested in pets? There is a very famous pets shop in the center of the ACM ci ...
- 【bzoj3291】Alice与能源计划 模拟费用流+二分图最大匹配
题目描述 在梦境中,Alice来到了火星.不知为何,转眼间Alice被任命为火星能源部长,并立刻面临着一个严峻的考验. 为了方便,我们可以将火星抽象成平面,并建立平面直角坐标系.火星上一共有N个居民点 ...
- 利用JS实现简单的瀑布流效果
哈哈, 我又来啦, 在这一段时间里, 我简单的学习了一下javascript(JS), 虽然不是很懂啦, 但是我也简单的尝试着做了点小东西, 就比如现在流行的瀑布流效果, 经过我的努力终于成功的完成了 ...
- 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 ...
- 经典网络流题目模板(P3376 + P2756 + P3381 : 最大流 + 二分图匹配 + 最小费用最大流)
题目来源 P3376 [模板]网络最大流 P2756 飞行员配对方案问题 P3381 [模板]最小费用最大流 最大流 最大流问题是网络流的经典类型之一,用处广泛,个人认为网络流问题最具特点的操作就是建 ...
- POJ2195 Going Home[费用流|二分图最大权匹配]
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22088 Accepted: 11155 Desc ...
- 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 ...
- 【BZOJ 3308】 3308: 九月的咖啡店 (费用流|二分图最大权匹配)
3308: 九月的咖啡店 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 244 Solved: 86 Description 深绘里在九份开了一家咖 ...
- hdu 3081(二分+并查集+最大流||二分图匹配)
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
随机推荐
- DNS+CDN
参考: 一起学DNS系列文章:http://jeffyyko.blog.51cto.com/28563/186026 CDN技术原理:http://kb.cnblogs.com/page/121664 ...
- hdu 3986(最短路变形好题)
Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/6553 ...
- asp.net mvc 自定义模型绑定
在asp.net mvc的控制器中如果能够活用模型的自动绑定功能的话能够减少许多工作量.但是如果我们想要对前台传来的数据进行一些处理再绑定到模型上,该怎么做呢? 这里用一个绑定用户数据的小案例来讲解a ...
- HDU 1223 还是畅通过程【最小生成树模板】
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 洛谷 P1865 A % B Problem[筛素数/前缀和思想/区间质数个数]
题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行,每行两个整数 l,r 表示区间 输出格式: 对于每次询问输出个数 t,如l或r∉[1,m]输出 Cros ...
- 机器学习(4):数据分析的工具-pandas的使用
前面几节说一些沉闷的概念,你若看了估计已经心生厌倦,我也是.所以,找到了一个理由来说一个有兴趣的话题,就是数据分析.是什么理由呢?就是,机器学习的处理过程中,数据分析是经常出现的操作.就算机器对大量样 ...
- 设置ListView的item不能点击
写了一个ListView结合volley的demo ListView只是用来展示数据,所以不需要点击效果. 网上搜索了下: 可以禁用ListView ListView.setEnabled(false ...
- Linux内核——内存管理
内存管理 页 内核把物理页作为内存管理的基本单位.内存管理单元(MMU,管理内存并把虚拟地址转换为物理地址)通常以页为单位进行处理.MMU以页大小为单位来管理系统中的页表. 从虚拟内存的角度看,页就是 ...
- Mockito 如何 mock 返回值为 void 的方法
转载:https://unmi.cc/mockito-how-to-mock-void-method/#more-7748 最初接触 Mockito 还思考并尝试过如何用它来 mock 返回值为 vo ...
- linux 文件删除恢复extundelete
首先要把删除文件所有磁盘分区卸载掉 然后安装yum install -y extundelete *2fs* extundelete /dev/sdb1 --inode #查看sdb1分区下删除的文件 ...