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 ...
随机推荐
- pyinstaller打包exe程序各种坑!!!
pyinstaller打包python成exe可执行程序,各种报错,各种坑,在次记录下 一.pyinstaller打包报错for real_module_name, six_moduleAttribu ...
- Liunx 重新mount
https://zhidao.baidu.com/question/349907351.html
- Rust-HayStack
src/main.rs extern crate multipart; extern crate iron; extern crate time; //image converter extern c ...
- (1)WCF托管
wcf 托管方式有很多种,常见的托管方式,iis,was,控制台,winfrom等. 先创建一个wcf服务 IService1.cs using System.ServiceModel; namesp ...
- Z划分空间
/* https://blog.csdn.net/fastkeeper/article/details/38905249 https://max.book118.com/html/2017/1007/ ...
- [Math Review] Linear Algebra for Singular Value Decomposition (SVD)
Matrix and Determinant Let C be an M × N matrix with real-valued entries, i.e. C={cij}mxn Determinan ...
- [USACO15JAN]踩踏Stampede
[USACO15JAN]踩踏Stampede 题目描述 DJ站在原点上向y轴正半轴看,然后有一群奶牛从他眼前飞过.这些奶牛初始都在第二象限,尾巴在(Xi,Yi),头在(Xi+1,Yi),每Ci秒向右走 ...
- 深入SQL SERVER 2000的内存管理机制
http://www.cnblogs.com/softj/articles/243591.html
- iOS申请证书,Certificates, Identifiers &Profiles 简介 - 申请证书
在真机调试以及发布应用时,要申请证书,我们必须知道Certificates, Identifiers ,Profiles 是什么含义,下面对它们做简单介绍,以及如果申请证书. Certificates ...
- 学习和家庭教育 z
大家好,我是王宁. 今天能站在这里,纯属偶然. 为什么说偶然呢? 因为,南雅是个人才济济的地方,164班是一个优秀的集体. 个人认为,班级前二十几名的同学,时机适宜,谁考班上第一名都有可能. 妈妈对我 ...