您的位置:首页 > Web前端

USACO Section 4.2 The Perfect Stall(二分图匹配)

2015-02-18 14:43 387 查看
#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=400+5;

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,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]<n) {
if(x==t) {
flow+=augment();
x=s;
}
int ok=0;
rep(i,cur[x],g[x].size()) {
edge e=edges[g[x][i]];
if(e.cap>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<<s()<<endl;

return 0;
}


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 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: