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 ...
随机推荐
- Java 基本日期类使用——格式化(二)
Java日期格式化主要有以下几种方式:java.text.DateFormat以及其子类java.text.SimpleDateFormat; DateFormat 是日期/时间格式化子类的抽象类,它 ...
- OC中的代理模式
OC中的代理模式,关于代理模式,如果还有同学不太清楚的话,就自己去补充知识了,这里就不做介绍了,这里只介绍OC中是如何实现代理模式的.这里举一个简单的例子:小孩类,护士类,保姆类,其中小孩类有两个方法 ...
- python-摩斯码转换
意义:简单实现摩斯码的破译和生成 代码: #-*- coding: UTF-8 -*- ' __date__ = '2016/2/2' import pprint import re chars = ...
- OpenCV学习 2:播放AVI视频
原创文章,欢迎转载,转载请注明出处 第二个程序,播放视频.用opencv做起来是如此的简单..哈哈. 学Opencv,只是为了在它的基础上实现工程应用,而它里面高深的理论我等屌丝只 ...
- QT显示GIF图片
在QT中要显示GIF图片,不能通过单单的添加部件来完成. 还需要手动的编写程序. 工具:QT Creator 新建一个工程,我们先在designer中,添加一个QLabel部件. 如下图: 将QLab ...
- 【Chromium中文文档】Chrom{e,ium}{,OS}中的硬件视频加速
Chrom{e,ium}{,OS}中的硬件视频加速 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//General_ ...
- Oracle EBS-SQL (BOM-1):检查供应类型错误.sql
--检查供应类型错误 SELECT MSI.SEGMENT1 物料编码, MSI.DESCRIPTION 物料描述, DECODE(MSI.WIP ...
- Oracle EBS-SQL (SYS-22):sysadmin_用户职责查询.sql
select fu.user_name 用户名, fu.description 用户说明, frv.RESPONSIBILITY_NAME 职责名称, REQUEST_GROUP_NAME 报表组, ...
- Enabling Process Accounting on Linux HOWTO
http://tldp.org/HOWTO/Process-Accounting/index.html
- ViewTreeObserver简介
Android ViewTreeObserver简介 一.结构 public final class ViewTreeObserver extends Object java.lang.Object ...