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 ...
随机推荐
- 了解MIP(Mobile Instant Pages)
mip官网:https://www.mipengine.org/ 什么是mip? mip是百度在2016年提出的移动网页加速器项目.可以简单理解为是一个规范. mip能做什么? mip能帮助站 ...
- 常用linq语法
1.简单的linq语法 var ss = from r in db.Am_recProScheme select r; var ss1 = db.Am_recProScheme; string sss ...
- pta 习题集 5-14 求n以内最大的k个素数以及它们的和
本题要求计算并输出不超过n的最大的k个素数以及它们的和. 输入格式: 输入在一行中给出n(10≤≤n≤≤10000)和k(1≤≤k≤≤10)的值. 输出格式: 在一行中按下列格式输出: 素数1+素数2 ...
- CH601后缀数组【Trie树】
内含字典树创建及查询模板 1601 前缀统计 0x10「基本数据结构」例题 描述 给定N个字符串S1,S2...SN,接下来进行M次询问,每次询问给定一个字符串T,求S1-SN中有多少个字符串是T的前 ...
- Kafka Producer接口
参考, https://cwiki.apache.org/confluence/display/KAFKA/0.8.0+Producer+Example http://kafka.apache.org ...
- ansible相关
上图为ansible的基本架构,从上图可以了解到其由以下部分组成: 核心:ansible 核心模块(Core Modules):这些都是ansible自带的模块 扩展模块(Custom Modules ...
- matplotlib-曲线和折线案例
matplotlib-曲线和折线案例 import matplotlib.pyplot as plt import numpy as np x = np.linspace(-5, 5, 100) pr ...
- 10行代码搞定移动web端自定义tap事件
发发牢骚 移动web端里摸爬滚打这么久踩了不少坑,有一定移动web端经验的同学一定被click困扰过.我也不列外.一路走来被虐的不行,fastclick.touchend.iscroll什么的都用过, ...
- POJ2992:Divisors(求N!因子的个数,乘性函数,分解n!的质因子(算是找规律))
题目链接:http://poj.org/problem?id=2992 题目要求:Your task in this problem is to determine the number of div ...
- C#字母转换成数字/数字转换成字母 - ASCII码转换
字母转换成数字 byte[] array = new byte[1]; //定义一组数组arrayarray = System.Text.Encoding.ASCII.GetBytes(strin ...