poj3281网络流之最大流
加一个源点和汇点,把每头牛拆成两个点,不拆点的话可能会出现多对食物与饮料被一个牛享用的情况,拆点后流量为1,不能同时通过了
然后用最大流处理,每个链接边都是1
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=,inf=; int c[N][N],pre[N];
bool vis[N];
int s,t,n,food,drink;
bool bfs()
{
memset(pre,,sizeof pre);
memset(vis,,sizeof vis);
vis[s]=;
queue<int>q;
q.push(s);
while(!q.empty()){
int k=q.front();
if(k==t)return ;
q.pop();
for(int i=;i<=n+n+food+drink+;i++)
{
if(!vis[i]&&c[k][i])
{
vis[i]=;
q.push(i);
pre[i]=k;
}
}
}
return ;
}
int max_flow()
{
int ans=;
while(){
if(!bfs())return ans;
int minn=;
for(int i=t;i!=s;i=pre[i])
minn=min(minn,c[pre[i]][i]);
for(int i=t;i!=s;i=pre[i])
{
c[pre[i]][i]-=minn;
c[i][pre[i]]+=minn;
}
ans+=minn;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
cin>>n>>food>>drink;
memset(c,,sizeof c);
for(int i=;i<=n;i++)
{
int a,b,k;
cin>>a>>b;
while(a--){
cin>>k;
c[k][i+food]=;
}
while(b--){
cin>>k;
c[i+n+food][k+n+n+food]=;
}
}
s=,t=n+n+food+drink+;
for(int i=;i<=n;i++)c[food+i][food+n+i]=;
for(int i=;i<=food;i++)//源点为0,汇点为n+food+drink+1
c[][i]=;
for(int i=;i<=drink;i++)
c[n+n+food+i][n+n+food+drink+]=;
cout<<max_flow()<<endl;
return ;
}
poj3281网络流之最大流的更多相关文章
- hdu 4940 Destroy Transportation system( 无源汇上下界网络流的可行流推断 )
题意:有n个点和m条有向边构成的网络.每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏全部S到T的路径的费用和 > 毁坏全部T到 ...
- 【Luogu2711】小行星(网络流,最大流)
[Luogu2711]小行星(网络流,最大流) 题面 题目描述 星云中有n颗行星,每颗行星的位置是(x,y,z).每次可以消除一个面(即x,y或z坐标相等)的行星,但是由于时间有限,求消除这些行星的最 ...
- 网络流之最大流Dinic算法模版
/* 网络流之最大流Dinic算法模版 */ #include <cstring> #include <cstdio> #include <queue> using ...
- 【BZOJ2324】[ZJOI2011]营救皮卡丘(网络流,费用流)
[BZOJ2324][ZJOI2011]营救皮卡丘(网络流,费用流) 题面 BZOJ 洛谷 题解 如果考虑每个人走的路径,就会很麻烦. 转过来考虑每个人破坏的点集,这样子每个人可以得到一个上升的序列. ...
- HDU 3416 Marriage Match IV (最短路径,网络流,最大流)
HDU 3416 Marriage Match IV (最短路径,网络流,最大流) Description Do not sincere non-interference. Like that sho ...
- HDU 3605 Escape (网络流,最大流,位运算压缩)
HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...
- HDU 3338 Kakuro Extension (网络流,最大流)
HDU 3338 Kakuro Extension (网络流,最大流) Description If you solved problem like this, forget it.Because y ...
- POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...
- UVA 10480 Sabotage (网络流,最大流,最小割)
UVA 10480 Sabotage (网络流,最大流,最小割) Description The regime of a small but wealthy dictatorship has been ...
随机推荐
- WebService 的简单使用
简单介绍 WebService是一种跨语言,跨进程,跨机器的数据交互技术. SOAP:简单对象访问协议,通过XML数据交互的轻量级协议,WebService就是采用的这种协议 WSDL:web服务描述 ...
- Windows:子线程中创建窗口
一般来讲,UI的所有操作都必须在主线程,否则会出现未知错误.但有时候我们会需要一个功能比较单一的窗口,同时希望他在一个单独的线程运行.并不影响主线程的效率. 下面说明一下新建子线程创建的新窗口的方法, ...
- 安装Anaconda3
wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh bash Anaconda3-5.0.1-Linux-x8 ...
- spring登录验证拦截器和根据用户角色登录
大家都知道spring的用户登录拦截器,确实省去了程序员不少的精力,下面说说我在项目中使用的感受. 德安微信管理后台是管理多个微信帐号的平台,登录到平台的用户有三个角色,游客和微信帐号管理员.超级管理 ...
- Hadoop MapReduce Task的进程模型与Spark Task的线程模型
Hadoop的MapReduce的Map Task和Reduce Task都是进程级别的:而Spark Task则是基于线程模型的. 多进程模型和多线程模型 所谓的多进程模型和多线程模型,指的是同一个 ...
- PAT 1073 Scientific Notation[字符串处理][科学记数法]
1073 Scientific Notation(20 分) Scientific notation is the way that scientists easily handle very lar ...
- Winzip和Winrar命令行的使用
Winzip和Winrar除了提供丰富灵活的图形界面操作功能外,二者都还具备从命令行模式进行文件的压缩与解压缩操作功能,极大的方便了我们在批处理文件中调用压缩软件.以下给出了二者的常用命令行使用方法: ...
- 安装WIN7时提示“缺少所需的CD/DVD驱动器设备驱动程序”
同事机器重装Win7,先百度了一下不适合64bit,于是直接上32bit系统. BOIS设置DVD启动,把安装盘放在移动光驱里,开始安装. 在安装时出现下图错误:“缺少所需的CD/DVD驱动器设备驱动 ...
- Python 自带IDLE 如何打开
- source insight 4.0的基本使用方法(转)
源:source insight 4.0的基本使用方法 source insight 4设置