POJ 3281 网络流dinic算法
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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 3
2 2 1 2 3 1
2 2 2 3 1 2
2 2 1 3 1 2
2 1 1 3 3
Sample Output
3
Hint
Cow 1: no meal
Cow 2: Food #2, Drink #2
Cow 3: Food #1, Drink #1
Cow 4: Food #3, Drink #3
The
pigeon-hole principle tells us we can do no better since there are only
three kinds of food or drink. Other test data sets are more
challenging, of course.
每组样例有3个数据,代表牛的数量,实物的数量,饮料的数量,每头牛都需要吃特定的食物和饮料,且只能吃一份,每种食物或者饮料被一头牛吃掉后不能再被其他的牛使用,问最多可以满足多少头牛
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int edge[][];//邻接矩阵
int dis[];//距源点距离,分层图
int start,end;
int m,n;//N:点数;M,边数
int bfs(){
memset(dis,-,sizeof(dis));//以-1填充
dis[]=;
queue<int>q;
q.push(start);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=;i<=n;i++){
if(dis[i]<&&edge[u][i]){
dis[i]=dis[u]+;
q.push(i); }
}
}
if(dis[n]>)
return ;
else
return ;//汇点的DIS小于零,表明BFS不到汇点
}
//Find代表一次增广,函数返回本次增广的流量,返回0表示无法增广
int find(int x,int low){//Low是源点到现在最窄的(剩余流量最小)的边的剩余流量
int a=;
if(x==n)
return low;//是汇点
for(int i=;i<=n;i++){
if(edge[x][i]>&&dis[i]==dis[x]+&&//联通,,是分层图的下一层
(a=find(i,min(low,edge[x][i])))){//能到汇点(a <> 0)
edge[x][i]-=a;
edge[i][x]+=a;
return a;
} }
return ;
}
int main(){
int a,b,c;
while(scanf("%d%d%d",&a,&b,&c)!=EOF){ n=a+a+b+c+;
memset(edge,,sizeof(edge));
for(int i=;i<=b;i++)
edge[][i]=;
for(int i=a+a+b+;i<=a+a+b+c;i++)
edge[i][n]=;
int u;
int sum1,sum2;
for(int i=;i<=a;i++){
// int u,v,w; scanf("%d%d",&sum1,&sum2);
for(int j=;j<=sum1;j++){
scanf("%d",&u);
edge[u][i+b]=;
}
for(int j=;j<=sum2;j++){
scanf("%d",&u);
edge[b+a+i][a+a+b+u]=;
} }
for(int i=;i<=a;i++){
edge[i+b][i+b+a]=; }
start=;
end=n;
int ans=;
while(bfs()){//要不停地建立分层图,如果BFS不到汇点才结束
ans+=find(,0x7fffffff);//一次BFS要不停地找增广路,直到找不到为止
}
printf("%d\n",ans);
}
return ;
}
POJ 3281 网络流dinic算法的更多相关文章
- POJ 3281 [网络流dinic算法模板]
题意: 农场主有f种食物,d种饮料,n头牛. 接下来的n行每行第一个数代表第i头牛喜欢吃的食物数量,和第i头牛喜欢喝的饮料数目. 接下来分别是喜欢的食物和饮料的编号. 求解:农场主最多能保证几头牛同时 ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- poj 1459 Power Network : 最大网络流 dinic算法实现
点击打开链接 Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 20903 Accepted: ...
- 网络流(dinic算法)
网络最大流(dinic) 模型 在一张图中,给定一个源点s,给定汇点t,点之间有一些水管,每条水管有一个容量,经过此水管的水流最大不超过容量,问最大能有多少水从s流到t(s有无限多的水). 解法 di ...
- 网络流Dinic算法
我的模板 例题: https://vjudge.net/problem/HDU-4280 struct Edge { int lst; int from; int to; int cap; int f ...
- POJ 3281 网络流 拆点保证本身只匹配一对食物和饮料
如何建图? 最开始的问题就是,怎么表示一只牛有了食物和饮料呢? 后来发现可以先将食物与牛匹配,牛再去和饮料匹配,实际上这就构成了三个层次. 起点到食物层边的容量是1,食物层到奶牛层容量是1,奶牛层到饮 ...
- poj 3281(网络流+拆点)
题目链接:http://poj.org/problem?id=3281 思路:设一个超级源点和一个超级汇点,源点与食物相连,饮料与汇点相连,然后就是对牛进行拆点,一边喜欢的食物相连,一边与喜欢的饮料相 ...
- POJ 1459 网络流 EK算法
题意: 2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 2 1 1 2 表示 共有2个节点,生产能量的点1个,消耗能量的点1个, 传递能量的通道2条:(0,1)20 (1,0) ...
- 高效的网络流dinic算法模版
#include <cstring> #include <algorithm> #include <vector> #define Maxn 120010 #def ...
随机推荐
- /etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc 的区别(转)
/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个运 ...
- CodeForces 459A Pashmak and Garden(水~几何-给两点求两点组成正方形)
题目链接:http://codeforces.com/problemset/problem/459/A 题目大意: 给出两个点(在坐标轴中),求另外两个点从而构成一个正方形,该正方形与坐标轴平行. 如 ...
- Eratosthenes筛选法构造1-n 素数表
筛选法:对于不超过n的每个非负整数p,删除2p,3p,4p...当处理完所有数之后,还没没删除的就是素数. 代码中进行了相应的优化. 本代码功能,输入一个数,输出从1-该数之间的素数.功能待完善,可将 ...
- Which is the best opencv or matlab for image processing?
http://www.researchgate.net/post/Which_is_the_best_opencv_or_matlab_for_image_processing Annette Mor ...
- ecshop Admin后台商品列表处(上架、下架、精品...取消精品)增加操作
相关文件:goods.php,goods_list.htm 思路: a.增添连接“转移仓库” b.在goods.php,读取仓库列表数据,并且实例化 c. 在goods_list.htm循环数据.点击 ...
- 谈谈我对PhoneGap的看法——(摘自唐巧的技术博客)
源地址:http://blog.devtang.com/blog/2012/03/24/talk-about-uiwebview-and-phonegap/ 主题部分 我认为PhoneGap有以下3大 ...
- 导航菜单:jQuery粘性滚动导航栏效果
粘性滚动是当导航在滚动过程中会占粘于浏览器上,达到方便网站页面浏览的效果,也是一种用户体验,下面我们看一下是怎么实现的: jQuery的 smint插件,也是一个导航菜单固定插件.当页滚动时,导航菜单 ...
- Node.SelectNodes
http://www.crifan.com/csharp_under_some_node_search_specific_child_node/ https://msdn.microsoft.com/ ...
- django 初级(一) 配置与周边
一.下载安装 从 https://www.djangoproject.com/download/ 下载最新的 django 版本,写本文时最新版本为 django 1.7,官方说只需要 python6 ...
- 2015年11月26日 Java基础系列(六)正则表达式Regex
package com.demo.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @autho ...