POJ3281 Dining 网络流建图
来源:互联网 发布:淘宝基础服务怎么设置 编辑:程序博客网 时间:2024/05/07 00:08
http://poj.org/problem?id=3281
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 the Di 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
题意:有n头牛f中食品d中饮料,每种牛都有自己喜欢的食品和饮料,问最多有多少头牛能够吃到自己喜欢的食品和饮料。
思路:这是一道最大流问题,难点在于如何建图。初学网络流借鉴了别人的思路。
#include<stdio.h>#include<algorithm>#include<vector>#include<string.h>using namespace std;const int maxm=500;const int inf=10000000;struct node{int to,cap,rev;};vector<node>v[maxm];bool used[maxm];int dfs(int s,int t,int f);int max_flow(int s,int t);int main(){int n,i,j,k,food,drink,ff,dd,x,y;while(scanf("%d%d%d",&n,&food,&drink)!=EOF){memset(v,0,sizeof(v));for(i=1;i<=n;i++){scanf("%d%d",&ff,&dd);while(ff--){scanf("%d",&x);x=2*n+x;v[x].push_back((node){i,1,v[i].size()});v[i].push_back((node){x,0,v[x].size()-1});}while(dd--){scanf("%d",&x);x=2*n+food+x;v[i+n].push_back((node){x,1,v[x].size()});v[x].push_back((node){i+n,0,v[i+n].size()-1});}}for(i=1;i<=n;i++){v[i].push_back((node){i+n,1,v[i+n].size()});v[i+n].push_back((node){i,0,v[i].size()-1});}for(i=2*n+1;i<=2*n+food;i++){v[0].push_back((node){i,1,v[i].size()});v[i].push_back((node){0,0,v[0].size()-1});}int e=2*n+food+drink+1;for(i=2*n+food+1;i<=2*n+food+drink;i++){v[i].push_back((node){e,1,v[e].size()});v[e].push_back((node){i,0,v[i].size()-1});}printf("%d\n",max_flow(0,e));}return 0;}int max_flow(int s,int t){int i,j,ans,d;ans=0;while(1){memset(used,false,sizeof(used));d=dfs(s,t,inf);if(d==0)return ans;ans+=d;}}int dfs(int s,int t,int f){int i,j;if(s==t)return f;used[s]=true;for(i=0;i<v[s].size();i++){node &temp=v[s][i];if(!used[temp.to] && temp.cap>0){int d=dfs(temp.to,t,min(temp.cap,f));if(d>0){temp.cap-=d;v[temp.to][temp.rev].cap+=d;return d;}}}return 0;}
- POJ3281 Dining 网络流建图
- POJ3281 网络流 dining
- 【POJ3281】Dining 网络流
- [网络流]poj3281 Dining
- POJ3281 Dining【网络流】
- POJ3281 Dining(网络流)
- poj3281 dining 网络流最大流算法
- poj3281 - Dining
- POJ3281--Dining
- poj3281 Dining
- poj3281 Dining
- POJ3281-Dining
- POJ3281 Dining
- POJ3281 Dining
- poj3281 Dining
- POJ3281:Dining
- 【网络流-最大流-Dinic-建模】POJ3281 Dining:Pascal 解法
- poj3281——Dining(网络流+拆点)
- 欢迎使用CSDN-markdown编辑器
- [最小割唯一性 Tarjan] BZOJ 1797 [Ahoi2009]Mincut 最小割
- android studio2.1.2中的jni调用
- [hdu3333 Turing Tree] 线段树离线 处理区间不同数
- 【华为 OJ 】整数与IP地址间的转换
- POJ3281 Dining 网络流建图
- 解决Centos7初次开机提示Initial setup of CentOS Linux 7
- web服务器lighttpd 对php的支持
- 文件中出现不显示的乱码 0xef 0xbb 0xbf
- JavaScript基本概念--函数
- Sparse
- 使用bootstrap进行表单测试
- HashMap源码解析 给jdk写注释系列之jdk1.6容器(4)
- SQL Tuning Set操作