二分图的最大匹配。我是用最大流求解。加个源点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(二分图匹配)的更多相关文章

  1. USACO Section 4.2: The Perfect Stall

    这题关键就在将题转换成最大流模板题.首先有一个原始点,N个cow个点, M个barn点和一个终点,原始点到cow点和barn点到终点的流都为1,而cow对应的barn就是cow点到对应barn点的流, ...

  2. POJ1274 The Perfect Stall[二分图最大匹配]

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23911   Accepted: 106 ...

  3. POJ1274 The Perfect Stall[二分图最大匹配 Hungary]【学习笔记】

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23911   Accepted: 106 ...

  4. 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall(二分图)

    P1894 [USACO4.2]完美的牛栏The Perfect Stall 题目描述 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星 ...

  5. POJ1274 The Perfect Stall 二分图,匈牙利算法

    N头牛,M个畜栏,每头牛仅仅喜欢当中的某几个畜栏,可是一个畜栏仅仅能有一仅仅牛拥有,问最多能够有多少仅仅牛拥有畜栏. 典型的指派型问题,用二分图匹配来做,求最大二分图匹配能够用最大流算法,也能够用匈牙 ...

  6. poj 1274 The Perfect Stall (二分匹配)

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17768   Accepted: 810 ...

  7. POJ-1274The Perfect Stall,二分匹配裸模板题

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23313   Accepted: 103 ...

  8. [POJ] 1274 The Perfect Stall(二分图最大匹配)

    题目地址:http://poj.org/problem?id=1274 把每个奶牛ci向它喜欢的畜栏vi连边建图.那么求最大安排数就变成求二分图最大匹配数. #include<cstdio> ...

  9. USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

    The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the ...

随机推荐

  1. 使用jquery-mockjax模拟ajax请求做前台測试

    一般来说,我们做web开发的时候前后台是分开做的,这样带来的优点是前台的开发者能够安心的写前台代码而后台的小伙伴就安心研究后台就OK了. 可是这样带来一个问题.当后台的小伙伴被天灾军团带走了,那前台须 ...

  2. easyui的样式easyui-textbox的一个bug

    easyui-testbox这个样式很恶心,用了这个就不能用传统的JQ来取值了,最近在使用上又发现了一个问题,就是赋值为0时,在输入框上会不显示,坑. <input class="ea ...

  3. 20141129 LinQ to SQL

    ORMO-Object对象R-Relation关系M-Mapping映射 对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是 ...

  4. jQuery EasyUI combobox多选和赋值

    定义select <select id="ID" name=empVO.acunid class="easyui-combobox" required=& ...

  5. memset memcpy函数

    memset 函数 1.其头文件为: #include<memory> 或者#include<string> 2.原型    看清是对每个字节,不是其类型 void *mems ...

  6. JavaWeb核心编程之使用Eclipse开发JavaWEB项目

    文章目录 1.eclipse切换到javaee项目 2.创建服务器(如果没有server选项, 怎么做) 3.定制新建面板内容 4.创建动态web工程 1.eclipse切换到javaee项目 如图 ...

  7. Silverlight代码编写对控件的PlaneProjection.RotationY属性控制动画

    Canvas c; void btnDraw_Click(object sender, RoutedEventArgs e) { Storyboard story = new Storyboard() ...

  8. linux select函数 shutdown函数

    #include<sys/select.h> #include<sys/time.h> int select(int maxfdp1,fd_set *readset,fd_se ...

  9. RelativeLayout布局下实现控件平分空间

    起源:使用惯LinearLayout的朋友都知道,若想实现对屏幕的等分,只需要设置Layout_weight的值即可. 可是在RelativeLayout布局下实现等分却不是那么容易. 下面就简单介绍 ...

  10. 美国地质调研局USGS

    https://lta.cr.usgs.gov/get_data/ http://www.usgs.gov/