Dining

poj3281:http://poj.org/problem?id=3281

题意:有n个人,然后有F份食物,D份饮料,然后每一个会有一些喜爱的饮料和食物,问你最多可以使得多少人同时得到一份自己喜爱的食物和一份饮料。

题解:很明显,要拆点。食物要拆成两个点,饮料也要拆成两个点,人也要拆成两个点,然后设置源点和汇点,源点和食物,食物和人,人和饮料,饮料和汇点分别建立一边。

  1 #include<iostream>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<cstdio>
  5 #include<queue>
  6 #define INF 100000000
  7 using namespace std;
  8 const int N=1205;
  9 const int M=1000000;
 10 struct Node{
 11    int v;
 12    int f;
 13    int next;
 14 }edge[M];
 15 int n,m,u,v,cnt,sx,ex;
 16 int head[N],pre[N];
 17 int val[N][40];//根据题目要求申请
 18 void init(){
 19     cnt=0;
 20     memset(head,-1,sizeof(head));
 21 }
 22 void add(int u,int v,int w){
 23     edge[cnt].v=v;
 24     edge[cnt].f=w;
 25     edge[cnt].next=head[u];
 26     head[u]=cnt++;
 27     edge[cnt].f=0;
 28     edge[cnt].v=u;
 29     edge[cnt].next=head[v];
 30     head[v]=cnt++;
 31 }
 32 bool BFS(){
 33   memset(pre,0,sizeof(pre));
 34   pre[sx]=1;
 35   queue<int>Q;
 36   Q.push(sx);
 37  while(!Q.empty()){
 38      int d=Q.front();
 39      Q.pop();
 40      for(int i=head[d];i!=-1;i=edge[i].next    ){
 41         if(edge[i].f&&!pre[edge[i].v]){
 42             pre[edge[i].v]=pre[d]+1;
 43             Q.push(edge[i].v);
 44         }
 45      }
 46   }
 47  return pre[ex]>0;
 48 }
 49 int dinic(int flow,int ps){
 50     int f=flow;
 51      if(ps==ex)return f;
 52      for(int i=head[ps];i!=-1;i=edge[i].next){
 53         if(edge[i].f&&pre[edge[i].v]==pre[ps]+1){
 54             int a=edge[i].f;
 55             int t=dinic(min(a,flow),edge[i].v);
 56               edge[i].f-=t;
 57               edge[i^1].f+=t;
 58             flow-=t;
 59              if(flow<=0)break;
 60         }
 61
 62      }
 63       if(f-flow<=0)pre[ps]=-1;
 64       return f-flow;
 65 }
 66 int solve(){
 67     int sum=0;
 68     while(BFS())
 69         sum+=dinic(INF,sx);
 70     return sum;
 71 }
 72 int d,f,k1,k2,temp;
 73 int main() {
 74     while(~scanf("%d%d%d",&n,&f,&d)){
 75         init();
 76     for(int i=1;i<=n;i++){
 77         scanf("%d%d",&k1,&k2);
 78         for(int j=1;j<=k1;j++){
 79               scanf("%d",&temp);
 80               add(2*n+2*d+f+temp,i,1);
 81         }
 82         for(int j=1;j<=k2;j++){
 83               scanf("%d",&temp);
 84               add(n+i,2*n+temp,1);
 85         }
 86         add(i,i+n,1);
 87     }
 88     for(int i=1;i<=f;i++){
 89        add(2*n+2*d+i,2*n+2*d+f+i,1);
 90        add(0,2*n+2*d+i,1);
 91     }
 92     for(int i=1;i<=d;i++){
 93         add(2*n+i,2*n+d+i,1);
 94         add(2*n+d+i,2*n+2*d+2*f+1,1);
 95     }
 96       sx=0;ex=2*n+2*d+2*f+1;
 97       printf("%d\n",solve());
 98     }
 99     return 0;
100 }

时间: 2024-07-29 15:32:55

Dining的相关文章

POJ 3281 Dining (最大流)

Dining Time Limit: 2000MS   Memory Limit: 65536K       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

BZOJ 1711: [Usaco2007 Open]Dining吃饭

1711: [Usaco2007 Open]Dining吃饭 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 902  Solved: 476[Submit][Status][Discuss] Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想让尽可能多的牛吃到他们喜欢的食品和饮料. 农夫JOHN做了F (1 <= F <= 100) 种食品

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(网络最大流)

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 最大流 Dinic算法

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10768   Accepted: 4938 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

ISAP最大流...果粉专用的最大流 Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9573   Accepted: 4417 Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John

Dining(最大流)

Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11844   Accepted: 5444 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(最大流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 (网络流最大流 拆点建图 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

poj3281 Dining

---恢复内容开始--- Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9587   Accepted: 4426 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 c