kuangbin专题专题十一 网络流 Dining POJ - 3281
题目链接:https://vjudge.net/problem/POJ-3281
题目:有不同种类的食物和饮料,每种只有1个库存,有N头牛,每头牛喜欢某些食物和某些饮料,但是一头牛
只能吃一种食物和喝一种饮料,问怎么分配食物和饮料才能让最多数量的牛饱餐。
思路:容易想到 食物->牛->饮料的流,当然一个牛可以被多个饮料流到,需要把牛拆成入点和出点,入点和出点流量为1,这样可以保证牛只吃或者喝某种食物和饮料,别的都流是套路,除了牛的分点之间流量为1,别的连接设置成1或者INF都一样,因为有牛的分点流量的限制。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std; const int N = ,INF = (int)1e9;
int n,F,D,tot,S,T;
int head[N],lev[N];
queue<int > que;
struct node{
int to,nxt,flow;
}e[N*N]; inline void add(int u,int v,int flow){
e[tot].to = v;
e[tot].flow = flow;
e[tot].nxt = head[u];
head[u] = tot++;
e[tot].to = u;
e[tot].flow = ;
e[tot].nxt = head[v];
head[v] = tot++;
} void build_map(int s,int t){ for(int i = s; i <= t; ++i) head[i] = -; tot = ;
//读入信息 0是源点 1~F食物 F+1~F+2*n牛 F+2*n+1~F+2*n+D饮料 F+2*n+D+1是汇点
int kind_f,kind_d,x;
for(int i = ; i <= n; ++i){
scanf("%d%d",&kind_f,&kind_d);
for(int j = ; j <= kind_f; ++j){
scanf("%d",&x);
add(x,F+i,);// add(F+i,x,0);
}
for(int j = ; j <= kind_d; ++j){
scanf("%d",&x);
add(F+n+i,F+*n+x,);// add(F+2*n+x,F+n+i,0);
}
}
for(int i = ; i <= F; ++i){
add(s,i,);// add(i,s,0);
}
for(int i = ; i <= D; ++i){
add(F+*n+i,t,);// add(t,F+2*n+i,0);
}
for(int i = ; i <= n; ++i){
add(F+i,F+n+i,);// add(F+n+i,F+i,0);
}
} void show(int s,int t){
for(int i = s; i <= t; ++i){
cout << "当前点为 " << i << " ";
cout << "能去到 ";
for(int o = head[i]; ~o; o = e[o].nxt){
printf(" %d 流量为 %d",e[o].to,e[o].flow);
}cout << endl;
}
} bool bfs(int s,int t){
while(!que.empty()) que.pop();
for(int i = s; i <= t; ++i) lev[i] = ;
lev[s] = ;
que.push(s);
while(!que.empty()){
int u = que.front(); que.pop();
for(int o = head[u]; ~o; o = e[o].nxt){
int v = e[o].to;
if(!lev[v] && e[o].flow ){
lev[v] = lev[u] + ;
if(v == t) return true;
que.push(v);
}
}
}
return false;
} int dfs(int now,int flow,int t){
if(now == t) return flow;
int to,sum = ,tmp;
for(int o = head[now]; ~o; o = e[o].nxt){
to = e[o].to;
if((lev[to] == lev[now] + ) && e[o].flow && (tmp = dfs(to,min(flow-sum,e[o].flow),t))){
e[o].flow -= tmp;
e[o^].flow += tmp;
if((sum += tmp) == flow) return sum;
}
}
return sum;
} int mf(int s,int t){
int _mf = ;
while(bfs(s,t)){
_mf += dfs(s,INF,t);
}
return _mf;
} int main(){ scanf("%d%d%d",&n,&F,&D);
S = ; T = F+*n+D+;
//建图
build_map(S,T);
// show(S,T); //图的显示
int ans = mf(S,T);
printf("%d\n",ans); return ;
}
kuangbin专题专题十一 网络流 Dining POJ - 3281的更多相关文章
- B - Dining POJ - 3281 网络流
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...
- B - Dining - poj 3281(最大流)
题目大意:有一群牛,还有一些牛喜欢的食物和喜欢的饮料,不过这些牛都很特别,他们不会与别的牛吃同一种食物或者饮料,现在约翰拿了一些食物和饮料,同时他也知道这些牛喜欢的食物和饮料的种类,求出来最多能让多少 ...
- Dining POJ - 3281
题意: f个食物,d杯饮料,每个牛都有想吃的食物和想喝的饮料,但食物和饮料每个只有一份 求最多能满足多少头牛.... 解析: 一道简单的无源汇拆点最大流 无源汇的一个最大流,先建立超级源s和超级汇 ...
- AC日记——Dining poj 3281
[POJ-3281] 思路: 把牛拆点: s向食物连边,流量1: 饮料向t连边,流量1: 食物向牛1连边,流量1: 牛2向饮料连边,流量1: 最大流: 来,上代码: #include <cstd ...
- [kuangbin带你飞]专题十一 网络流
ID Origin Title 34 / 81 Problem A POJ 3436 ACM Computer Factory 92 / 195 Problem B POJ 3 ...
- Kuangbin 带你飞专题十一 网络流题解 及模版 及上下界网络流等问题
首先是几份模版 最大流:虽然EK很慢但是优势就是短.求最小割的时候可以根据增广时的a数组来判断哪些边是割边.然而SAP的最大流版我只会套版,并不知道该如何找到这个割边.在尝试的时候发现了一些问题.所以 ...
- poj 3281 Dining 网络流-最大流-建图的题
题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...
- POJ 3281 Dining (网络流)
POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...
- POJ 3281 网络流dinic算法
B - Dining Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
随机推荐
- url查找参数
function GetUrlParam(paraName) { var url = document.location.toString(); var arrObj = url.split(&quo ...
- ASP.NET MVC 实现页落网资源分享网站+充值管理+后台管理(开篇)
源码下载地址:http://www.yealuo.com/Sccnn/Detail?KeyValue=c891ffae-7441-4afb-9a75-c5fe000e3d1c 这是一个比较简单 ...
- js实现bind
Function.prototype.bind=function(ctx,...lastArgs){ let self=this return (...laterArgs)=>self.appl ...
- Codeforces Round #524 (Div. 2) codeforces 1080A~1080F
目录 codeforces1080A codeforces 1080B codeforces 1080C codeforces 1080D codeforces 1080E codeforces 10 ...
- C++中常量成员函数的含义
C++中常量成员函数的含义 本文内容来源:<C++必知必会> 使用常量成员函数可以改变对象的逻辑状态,虽然对象的物理状态没有发生改变.考虑如下代码,它定义了一个类X: class X{ p ...
- win7下Oracle库imp导入dmp
第一步:创建备份文件存储目录 create or replace directory back_file as 'F:\APP\ADMINISTRATOR\ORADATA\ORCL'; create ...
- TDengine 时序数据库的 ADO.Net Core 提供程序 Maikebing.EntityFrameworkCore.Taos
简介 Entity, Framework, EF, Core, Data, O/RM, entity-framework-core,TDengine Maikebing.Data.Taos 是一个基于 ...
- 微信小程序获取多个input和textarea的值(es6加微信小程序APi)
wxml js 俩行即可解决,money1,money2,money3
- 18.函数复习,文件处理b模式(二进制处理),文件处理其他高级玩法
1.函数复习 # map # l = [1,2,3,4,5] # print(list(map(str,l))) # reduce # l = [1,2,3,4,5] # from functools ...
- 亲测可用的优雅的在已经安装了python的Ubuntu上安装python3.5
我的Ubuntu上已经安装了python2.7和3.4. 用以下方法可以方便的顺利的安装python3.5,使用的时候也不会发生冲突. 一条一条输入以下语句 wget https://www.pyth ...