博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
USACO Section 4.2 The Perfect Stall(二分图匹配)
阅读量:4679 次
发布时间:2019-06-09

本文共 3260 字,大约阅读时间需要 10 分钟。

二分图的最大匹配。我是用最大流求解。加个源点s和汇点t;s和每只cow、每个stall和t 连一条容量为1有向边,每只cow和stall(that the cow is willing to produce milk in )也连一条容量为1的边。然后就用ISAP。

#include
#include
#include
#include
#include
#define rep(i,l,r) for(int i=l;i
edges; vector
g[maxn]; int d[maxn]; int cur[maxn]; int p[maxn]; int num[maxn]; void init(int n) { this->n=n; rep(i,0,n) g[i].clear(); edges.clear(); clr(d,0); clr(num,0); clr(cur,0); rep(i,0,n) num[d[i]]++; } void addEdge(int from,int to,int cap) { edges.push_back((edge){ from,to,cap,0}); edges.push_back((edge){to,from,0,0,}); m=edges.size(); g[from].push_back(m-2); g[to].push_back(m-1); } 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]^1].flow-=a; x=edges[p[x]].from; } return a; } int maxFlow(int s,int t) { this->s=s; this->t=t; int flow=0; int x=s; while(d[s]
e.flow && d[x]==d[e.to]+1) { ok=1; p[e.to]=g[x][i]; cur[x]=i; x=e.to; break; } } if(!ok) { int m=n-1; rep(i,0,g[x].size()) { edge e=edges[g[x][i]]; if(e.cap>e.flow) m=min(m,d[e.to]); } if(--num[d[x]]==0) break; num[d[x]=m+1]++; cur[x]=0; if(x!=s) x=edges[p[x]].from; } } return flow; }} isap;int s() { int n,m; cin>>n>>m; isap.init(n+m+2); rep(i,0,n) { int t; scanf("%d",&t); isap.addEdge(0,i+1,1); rep(j,0,t) { int h; scanf("%d",&h); h+=n; isap.addEdge(i+1,h,1); } } rep(i,0,m) { int x=i+n+1; isap.addEdge(x,m+n+1,1) ; } return isap.maxFlow(0,n+m+1);}int main() { freopen("stall4.in","r",stdin); freopen("stall4.out","w",stdout); cout<
<
View Code
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 52 2 53 2 3 42 1 53 1 2 51 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

转载于:https://www.cnblogs.com/JSZX11556/p/4295764.html

你可能感兴趣的文章
威尔逊定理及证明
查看>>
[LeetCode] Peeking Iterator
查看>>
Understanding Unix/Linux Programming-用户程序play_again4.c
查看>>
算法总结
查看>>
WPF中使用USERCONTROL
查看>>
图片,base64 互转
查看>>
cache—主存—辅存三级调度模拟
查看>>
Java线程的定义
查看>>
Python-面向对象(组合、封装与多态)
查看>>
Mininet
查看>>
COSC2531 Programming Fundamentals
查看>>
设计模式系列 - 访问者模式
查看>>
20180507小测
查看>>
eclipse左侧不见
查看>>
python会缓存小的整数和短小的字符
查看>>
格网与四叉树索引
查看>>
多张照片拍摄、图片浏览
查看>>
html(5) css
查看>>
Azure Web连接到Azure MySql Db
查看>>
Linux shell 命令判断执行语法 ; , && , ||
查看>>