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=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
posted @ 2015-02-18 14:43  JSZX11556  阅读(285)  评论(0编辑  收藏  举报