POJ 3281-Dining(网络流_最大流_ISAP算法+拆点)
来源:互联网 发布:手机扫描扑克牌软件 编辑:程序博客网 时间:2024/06/03 03:46
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 he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.
Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.
Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).
Input
Lines 2..N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and theDi integers following that denote the drinks that cow i will drink.
Output
Sample Input
4 3 32 2 1 2 3 12 2 2 3 1 22 2 1 3 1 22 1 1 3 3
Sample Output
3
题意:有F种食物,D种饮料,N头奶牛,只能吃某种食物和饮料(而且只能吃特定的一份),一种食物被一头牛吃了之后,其余牛就不能吃了
第一行有N,F,D三个整数:接着2-N+1行代表第i头牛,前面两个整数是Fi与Di(食物与饮料的种类数量),接着是食物的种类与饮料的种类
要求输出最多分配能够满足的牛的数量.
思路:这是一种神奇的建图方式-拆点。让我想打死都想不出来。sad
建图,有2*n+f+d+2个顶点,0表示源点,2*n+f+d+1表示汇点。
由源点指向食物,再由食物指向牛,牛再指向对应的饮料,饮料再指向汇点
当然要使每一头牛都对应每一份食物与饮料,所以应该牛i指向牛i再指向饮料,这样就可以避免一头牛只占用多份食物与饮料了
全部是有向的边,而且权值全部为1
我在这里是1到f为食物点,f+1到f+2*n为牛点,f+2*n+1到f+2*n+d为饮料点
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#include <map>#include <set>#include <queue>#include <stack>using namespace std;const int inf=0x3f3f3f3f;int head[1010],num[1010],d[1010],cur[1010],q[1010],pre[1010];int cnt,n,s,t,nv;int maxint=inf;struct node{ int u,v,cap; int next;}edge[100010];void add(int u,int v,int cap){ edge[cnt].v=v; edge[cnt].cap=cap; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].cap=0; edge[cnt].next=head[v]; head[v]=cnt++;}void bfs(){ int i,j; memset(num,0,sizeof(num)); memset(d,-1,sizeof(d)); int f1=0,f2=0; q[f1++]=t; num[0]=1; d[t]=0; while(f1>=f2){ int u=q[f2++]; for(i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(d[v]!=-1) continue; d[v]=d[u]+1; num[d[v]]++; q[f1++]=v; } }}void isap(){ memcpy(cur,head,sizeof(cur)); bfs(); int flow=0,u=pre[s]=s,i; while(d[s]<nv){ if(u==t){ int f=maxint,pos; for(i=s;i!=t;i=edge[cur[i]].v){ if(f>edge[cur[i]].cap){ f=edge[cur[i]].cap; pos=i; } } for(i=s;i!=t;i=edge[cur[i]].v){ edge[cur[i]].cap-=f; edge[cur[i]^1].cap+=f; } flow+=f; u=pos; } for(i=cur[u];i!=-1;i=edge[i].next){ if(d[edge[i].v]+1==d[u]&&edge[i].cap) break; } if(i!=-1){ cur[u]=i; pre[edge[i].v]=u; u=edge[i].v; } else{ if(--num[d[u]]==0) break; int mind=nv; for(i=head[u];i!=-1;i=edge[i].next){ if(mind>d[edge[i].v]&&edge[i].cap){ cur[u]=i; mind=d[edge[i].v]; } } d[u]=mind+1; num[d[u]]++; u=pre[u]; } } printf("%d\n",flow);}int main(){ int i,j; int m,f,d,a,b,c; scanf("%d %d %d",&m,&f,&d); memset(head,-1,sizeof(head)); cnt=0; s=0; t=2*m+f+d+2+1; nv=t+1; for(i=1;i<=f;i++){ add(0,i,1);//源点指向食物 } for(i=1;i<=d;i++){ add(i+2*m+f,t,1);//饮料指向汇点 } for(i=1;i<=m;i++){ add(f+i,f+m+i,1);//牛指向牛 } //点的顺序:食物,奶牛,饮料 for(i=1;i<=m;i++){ scanf("%d %d",&a,&b); while(a--){ scanf("%d",&c); add(c,i+f,1);//食物指向牛 } while(b--) { scanf("%d",&c); add(i+f+m,c+f+2*m,1);//牛指向饮料 } } isap(); return 0;}
- POJ 3281-Dining(网络流_最大流_ISAP算法+拆点)
- POJ 1273-Drainage Ditches(网络流_最大流_ISAP()算法和EK()算法)
- 拆点最大流-POJ-3281-Dining
- poj 3281 Dining(拆点+最大流)
- POJ---3281 Dining【拆点最大流】
- Poj 3281 Dining (拆点网络流)
- POJ 3281 Dining 拆点网络流
- POJ 3281 Dining (拆点+ 网络流)
- poj 3281 Dining 【拆点网络流】【最大流经典问题 关键建图】
- POJ 3281 - Dining(网络流‘最大流)
- poj 3281 Dining(网络流最大流)
- poj 3281 Dining 最大网络流
- POJ 3281 Dining(网络最大流)
- POJ 3281 Dining 匹配,网络最大流
- poj 3281 Dining(最大流+拆点)
- poj 3281 Dining(最大流基础,拆点)
- POJ 3281 Dining(拆点+最大流)
- 网络流( ISAP + 拆点 )——Dining ( POJ 3281 )
- Linux安装xdebug.(Debian,Ubuntu)
- css视频笔记整理
- 项目管理的本质
- 计算机组成原理总结
- http://blog.sciencenet.cn/blog-630522-491356.html
- POJ 3281-Dining(网络流_最大流_ISAP算法+拆点)
- brew update gives Error: Failed to update tap: homebrew/dupes
- android 基础知识(一)
- Multiply Strings -- leetcode
- VirtualBox共享文件夹设置
- [SharePoint 2010]为SharePoint 2010配置基于表单的身份认证
- 算法竞赛入门经典(第2版)习题4-8 特别困的学生UVa12108
- Andrew NG: 机器学习公开课纪要
- 学习Postgresql数据库