POJ 3281 Dining(网络流拆点)

【题目链接】 http://poj.org/problem?id=3281

【题目大意】

  给出一些食物,一些饮料,每头牛只喜欢一些种类的食物和饮料,
  但是每头牛最多只能得到一种饮料和食物,问可以最多满足几头牛的要求
  即同时得到喜欢的饮料和食物

【题解】

  建立一个源点连接食物,汇点连接饮料,中间连接牛,
  为了防止同一头牛占用多个资源,所以我们对牛进行拆点,限流为1.

【代码(Isap)】

#include <cstdio>
#include <cstring>
using namespace std;
const int N=30010,inf=~0U>>2;
struct edge{int t,f;edge*nxt,*pair;}*g[N],*d[N],pool[N],*cur=pool;
int sum,ff,dd,x,fi,di,cnt,cas,i,u,v,cost,n,m,S,T,h[N],gap[N],maxflow;
int min(int x,int y){return x<y?x:y;}
void add(int s,int t,int w){
    edge*p=cur++;p->t=t;p->f=w;p->nxt=g[s];g[s]=p;
    p=cur++;p->t=s;p->f=0;p->nxt=g[t];g[t]=p;
    g[s]->pair=g[t];g[t]->pair=g[s];
}
int sap(int v,int flow){
    if(v==T)return flow; int rec=0;
    for(edge*p=d[v];p;p=p->nxt)if(h[v]==h[p->t]+1&&p->f){
        int ret=sap(p->t,min(flow-rec,p->f));
        p->f-=ret;p->pair->f+=ret;d[v]=p;
        if((rec+=ret)==flow)return flow;
    }if(!(--gap[h[v]]))h[S]=T;
    gap[++h[v]]++;d[v]=g[v];
    return rec;
}
int main(){
    while(~scanf("%d%d%d",&n,&fi,&di)){
        maxflow=0; sum=n+fi+di+n+2; S=sum-1; T=sum;
        for(cur=pool,i=sum;i<=sum;i++)g[i]=d[i]=NULL,h[i]=gap[i]=0;
        for(i=1;i<=n;i++){
            scanf("%d%d",&ff,&dd);
            for(int j=0;j<ff;j++){scanf("%d",&x);add(x+n,i,1);}
            for(int j=0;j<dd;j++){scanf("%d",&x);add(i+n+fi+di,x+n+fi,1);}
            add(i,i+n+fi+di,1);
        }for(int i=1;i<=fi;i++)add(S,i+n,1);
        for(int i=1;i<=di;i++)add(i+n+fi,T,1);
        for(gap[0]=T,i=1;i<=T;i++)d[i]=g[i];
        while(h[S]<T)maxflow+=sap(S,inf);
        printf("%d\n",maxflow);
    }return 0;
}

【代码(Dinic)】

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAX_V=500;
struct edge{int to,cap,rev;};
vector<edge> G[MAX_V];
int level[MAX_V],iter[MAX_V];
void add_edge(int from,int to,int cap){
    G[from].push_back((edge){to,cap,G[to].size()});
    G[to].push_back((edge){from,0,G[from].size()-1});
}
void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
        int v=que.front(); que.pop();
        for(int i=0;i<G[v].size();i++){
            edge &e=G[v][i];
            if(e.cap>0&&level[e.to]<0){
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}
int dfs(int v,int t,int f){
    if(v==t)return f;
    for(int &i=iter[v];i<G[v].size();i++){
        edge &e=G[v][i];
        if(e.cap>0&&level[v]<level[e.to]){
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }return 0;
}
int max_flow(int s,int t){
    int flow=0;
    for(;;){
        bfs(s);
        if(level[t]<0)return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(s,t,INF))>0){
            flow+=f;
        }
    }
}
const int MAX_N=100;
const int MAX_F=100;
const int MAX_D=100;
int N,F,D;
bool likeF[MAX_N][MAX_F];
bool likeD[MAX_N][MAX_D];
void init(){
    int fi,di;
    memset(likeF,0,sizeof(likeF));
    memset(likeD,0,sizeof(likeD));
    for(int i=0;i<N;i++){
        scanf("%d%d",&fi,&di);
        for(int j=0;j<fi;j++){
            int x;scanf("%d",&x);
            likeF[i][x-1]=1;
        }
        for(int j=0;j<di;j++){
            int x;scanf("%d",&x);
            likeD[i][x-1]=1;
        }
    }
}
void solve(){
    int s=N*2+F+D,t=s+1;
    for(int i=0;i<=t;i++)G[i].clear();
    for(int i=0;i<F;i++)add_edge(s,N*2+i,1);
    for(int i=0;i<D;i++)add_edge(N*2+F+i,t,1);
    for(int i=0;i<N;i++){
        add_edge(i,N+i,1);
        for(int j=0;j<F;j++){
            if(likeF[i][j])add_edge(N*2+j,i,1);
        }
		for(int j=0;j<D;j++){
            if(likeD[i][j])add_edge(N+i,N*2+F+j,1);
        }
	}printf("%d\n",max_flow(s,t));
}
int main(){
    while(~scanf("%d%d%d",&N,&F,&D)){
        init();
        solve();
    }return 0;
}
时间: 2024-11-13 13:11:41

POJ 3281 Dining(网络流拆点)的相关文章

POJ 3281 Dining (网络流最大流 拆点建图 Edmonds-Karp算法)

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10159   Accepted: 4676 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulo

POJ 3281 Dining (网络流之最大流)

题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料.每头牛都有各自喜欢的食物和饮料, 而每种食物或饮料只能分配给一头牛.最多能有多少头牛可以同时得到喜欢的食物和饮料? 析:是一个经典网络流的题,建立一个超级源点,连向每种食物,建立一个超级汇点,连向每种饮料,然后把每头牛拆成两个点, 一个和食物连,一个和饮料连,最后跑一遍最大流即可. 代码如下: #pragma comment(linker, "/STACK:

POJ 3281 Dining(最大流建图 &amp;&amp; ISAP &amp;&amp; 拆点)

题目链接:http://poj.org/problem?id=3281 努力练建图ing!!! 题意:有 N 头牛,有 F 种食物和 D 种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料. 第2行-第N+1行.是牛i 喜欢A种食物,B种饮料,及食物种类列表和饮料种类列表. 问最多能使几头牛同时享用到自己喜欢的食物和饮料.->最大流. 本题难点是建图: 思路:一般都是左边一个集合表示源点与供应相连,右边一个集合表示需求与汇点相连. 但是本题,牛作为需求仍然是一个群体,但是供

POJ 3281 Dining(网络最大流)

http://poj.org/problem?id=3281 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9121   Accepted: 4199 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.

poj 3281 Dining(最大流)

poj 3281 Dining Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their prefer

POJ 3281 Dining(最大流)

POJ 3281 Dining 题目链接 题意:n个牛,每个牛有一些喜欢的食物和饮料,每种食物饮料只有一个,问最大能匹配上多少只牛每个牛都能吃上喜欢的食物和喜欢的饮料 思路:最大流,建模源点到每个食物连一条边,容量为1,每个饮料向汇点连一条边容量为1,然后由于每个牛有容量1,所以把牛进行拆点,然后食物连向牛的入点,牛的出点连向食物,跑一下最大流即可 代码: #include <cstdio> #include <cstring> #include <queue> #in

POJ 3281 Dining(最大流dinic&amp;&amp;SAP)

Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although

poj 3281 Dining(网络流+拆点)

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20052   Accepted: 8915 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulo

poj 3281 Dining (最大网络流)

题目链接: http://poj.org/problem?id=3281 题目大意: 有n头牛,f种食物,d种饮料,第i头牛喜欢fi种食物和di种饮料,每种食物或者饮料被一头牛选中后,就不能被其他的牛选了,问最多能满足多少头牛的要求? 解题思路: 最大匹配问题,关键在于如何建图,可以虚构出来一个源点,一个汇点,一共需要f+d+2*n+2个点即可,建图为:源点—>食物—>牛—>牛—>饮料—> 汇点.把牛作为点拆开建图是为了让一头牛只对应一种饮料和一种食物,避免出现对应多种饮料或