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 ...
随机推荐
- Wannafly交流赛1 B 硬币[数学思维/贪心]
链接:https://www.nowcoder.com/acm/contest/69/B来源:牛客网 蜥蜴的生日快到了,就在这个月底! 今年,蜥蜴的快乐伙伴之一壁虎想要送好多个1元硬币来恶整蜥蜴. 壁 ...
- 关于js延迟加载(异步操作)的方式
一.概述 最近重新开始学习js,在第一章的一个小节里写到了“脚本调用策略”,书上写的这部分不多,但是发现在我之前的(笔)面试中,问到的频率还是比较高的.自己一直习惯于直接把所有js文件写在head里, ...
- 洛谷——P1977 出租车拼车
题目背景 话说小 x 有一次去参加比赛,虽然学校离比赛地点不太远,但小 x 还是想坐 出租车去.大学城的出租车总是比较另类,有“拼车”一说,也就是说,你一个人 坐车去,还是一堆人一起,总共需要支付的钱 ...
- Jenkins强制设置语言为中文
解决方法如下: 1.使用中文版的google浏览器,并确定把语言设置成了中文.语言配置在设置页.如下: 2.直接设置jenkins的语言.强制性. 前提:先安装插件:Locale plugin 配置如 ...
- 跳转移动端js代码
<script language="JavaScript"> $(function(){ var MobileUA = (function() { var ua = n ...
- Data-structures-and-algorithms-interview-questions-and-their-solutions
https://techiedelight.quora.com/500-Data-structures-and-algorithms-interview-questions-and-their-sol ...
- Ubuntu下sudo apt-get install vim 失败的解决办法
Ubuntu下 执行命令:sudo apt-get install vim 失败 解决办法: 更新一下,命令:sudo apt-get update 再安装即可成功:sudo apt-get inst ...
- 危急,不要任意让站点记住password自己主动登陆!
为了方便用户登录,差点儿全部的站点都实现了"记住password"."自己主动登陆"这样似乎人性化的功能. 我也非常喜欢这个功能,由于我自己的脑子实在是讨厌记东 ...
- 关于 Shiro 的权限匹配器和过滤器
项目源码:https://github.com/weimingge14/Shiro-project演示地址:http://liweiblog.duapp.com/Shiro-project/login ...
- Hibernate之load和get的差别
load和get都会能够起到从数据库中获取持久态数据的作用.可是还有些略微的差别的. 參考以下的这个样例: @Test(expected = IllegalArgumentException.clas ...