USACO Section 4.2 The Perfect Stall(二分图匹配)
二分图的最大匹配。我是用最大流求解。加个源点s和汇点t;s和每只cow、每个stall和t 连一条容量为1有向边,每只cow和stall(that the cow is willing to produce milk in )也连一条容量为1的边。然后就用ISAP。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector> #define rep(i,l,r) for(int i=l;i<r;i++)
#define clr(x,c) memset(x,c,sizeof(x)) using namespace std; const int inf=0x3f3f3f3f,maxn=+; struct edge {
int from,to,cap,flow;
}; struct ISAP {
int n,m,s,t;
vector<edge> edges;
vector<int> g[maxn];
int d[maxn];
int cur[maxn];
int p[maxn];
int num[maxn]; void init(int n) {
this->n=n;
rep(i,,n) g[i].clear();
edges.clear();
clr(d,);
clr(num,);
clr(cur,);
rep(i,,n) num[d[i]]++;
} void addEdge(int from,int to,int cap) {
edges.push_back((edge){from,to,cap,});
edges.push_back((edge){to,from,,,});
m=edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
} int augment() {
int x=t,a=inf;
while(x!=s) {
edge e=edges[p[x]];
a=min(a,e.cap-e.flow);
x=edges[p[x]].from;
}
x=t;
while(x!=s) {
edges[p[x]].flow+=a;
edges[p[x]^].flow-=a;
x=edges[p[x]].from;
}
return a;
} int maxFlow(int s,int t) {
this->s=s; this->t=t;
int flow=;
int x=s;
while(d[s]<n) {
if(x==t) {
flow+=augment();
x=s;
}
int ok=;
rep(i,cur[x],g[x].size()) {
edge e=edges[g[x][i]];
if(e.cap>e.flow && d[x]==d[e.to]+) {
ok=;
p[e.to]=g[x][i];
cur[x]=i;
x=e.to;
break;
}
}
if(!ok) {
int m=n-;
rep(i,,g[x].size()) {
edge e=edges[g[x][i]];
if(e.cap>e.flow) m=min(m,d[e.to]);
}
if(--num[d[x]]==) break;
num[d[x]=m+]++;
cur[x]=;
if(x!=s) x=edges[p[x]].from;
}
}
return flow;
}
} isap; int s() {
int n,m;
cin>>n>>m;
isap.init(n+m+);
rep(i,,n) {
int t;
scanf("%d",&t);
isap.addEdge(,i+,);
rep(j,,t) {
int h;
scanf("%d",&h);
h+=n;
isap.addEdge(i+,h,);
}
}
rep(i,,m) {
int x=i+n+;
isap.addEdge(x,m+n+,) ;
}
return isap.maxFlow(,n+m+);
} int main() {
freopen("stall4.in","r",stdin);
freopen("stall4.out","w",stdout); cout<<s()<<endl; return ;
}
The Perfect Stall
Hal Burch
Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John randomly assigned cows to stalls, but it quickly became clear that any given cow was only willing to produce milk in certain stalls. For the last week, Farmer John has been collecting data on which cows are willing to produce milk in which stalls. A stall may be only assigned to one cow, and, of course, a cow may be only assigned to one stall.
Given the preferences of the cows, compute the maximum number of milk-producing assignments of cows to stalls that is possible.
PROGRAM NAME: stall4
INPUT FORMAT
Line 1: | One line with two integers, N (0 <= N <= 200) and M (0 <= M <= 200). N is the number of cows that Farmer John has and M is the number of stalls in the new barn. |
Line 2..N+1: | N lines, each corresponding to a single cow. The first integer (Si) on the line is the number of stalls that the cow is willing to produce milk in (0 <= Si <= M). The subsequent Si integers on that line are the stalls in which that cow is willing to produce milk. The stall numbers will be integers in the range (1..M), and no stall will be listed twice for a given cow. |
SAMPLE INPUT (file stall4.in)
5 5
2 2 5
3 2 3 4
2 1 5
3 1 2 5
1 2
OUTPUT FORMAT
A single line with a single integer, the maximum number of milk-producing stall assignments that can be made.
SAMPLE OUTPUT (file stall4.out)
4
USACO Section 4.2 The Perfect Stall(二分图匹配)的更多相关文章
- USACO Section 4.2: The Perfect Stall
这题关键就在将题转换成最大流模板题.首先有一个原始点,N个cow个点, M个barn点和一个终点,原始点到cow点和barn点到终点的流都为1,而cow对应的barn就是cow点到对应barn点的流, ...
- POJ1274 The Perfect Stall[二分图最大匹配]
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23911 Accepted: 106 ...
- POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23911 Accepted: 106 ...
- 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)
P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...
- POJ1274 The Perfect Stall 二分图,匈牙利算法
N头牛,M个畜栏,每头牛仅仅喜欢当中的某几个畜栏,可是一个畜栏仅仅能有一仅仅牛拥有,问最多能够有多少仅仅牛拥有畜栏. 典型的指派型问题,用二分图匹配来做,求最大二分图匹配能够用最大流算法,也能够用匈牙 ...
- poj 1274 The Perfect Stall (二分匹配)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17768 Accepted: 810 ...
- POJ-1274The Perfect Stall,二分匹配裸模板题
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23313 Accepted: 103 ...
- [POJ] 1274 The Perfect Stall(二分图最大匹配)
题目地址:http://poj.org/problem?id=1274 把每个奶牛ci向它喜欢的畜栏vi连边建图.那么求最大安排数就变成求二分图最大匹配数. #include<cstdio> ...
- USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)
The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...
随机推荐
- 畅通工程续(dijskra+SPFA)
畅通工程续 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- 【LeetCode】4Sum 解题报告
[题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- SQL如何合并查询结果
1.UNION的作用 UNION 指令的目的是将两个 SQL 语句的结果合并起来.从这个角度来看, UNION 跟 JOIN 有些许类似,因为这两个指令都可以由多个表格中撷取资料. UNION 的一个 ...
- pagination分页插件
最近做了个项目,有用到分页, 这类插件应该是很常用的, 虽然网上很多现成的分页插件, 但是还是想着自己写一个, 给自己积累点东西, 顺便练练手, 写了差不多3个小时左右, 代码如下: 代码: < ...
- AngularJs 常用函数
/** * [intersect 取两个数组的交集] var firstArray = [1,3,5]; var secondArray = [2,5,8]; var result */ .filte ...
- Oracle EBS DBA常用SQL - 安装/补丁【Z】
Oracle EBS DBA常用SQL - 安装/补丁 检查应用补丁有没有安装:select bug_number,last_update_date from ad_bugs where bug_nu ...
- 对List对象按照某个成员变量进行排序
/** * 对List对象按照某个成员变量进行排序 * @param list List对象 * @param sortField 排序的属性名称 * @param sortMode 排序方式:ASC ...
- Search Bar 去掉背景颜色
storyboard里只能设置background颜色,可是发现clear color无法使用 其实代码还是可以设置的,那就是删除背景view [[self.searchBar.subviews ob ...
- floyd学习中
#include<iostream> #include<math.h> #include<stdio.h> using namespace std; //long ...
- BZOJ 1570: [JSOI2008]Blue Mary的旅行( 二分答案 + 最大流 )
二分答案, 然后对于答案m, 把地点分成m层, 对于边(u, v), 第x层的u -> 第x+1层的v 连边. 然后第x层的u -> 第x+1层的u连边(+oo), S->第一层的1 ...