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 ...
随机推荐
- dotnet 通过 WMI 获取系统补丁
本文告诉大家如何通过 WMI 获取补丁 通过 Win32_QuickFixEngineering 可以获取系统启动的服务 下面代码只是获取补丁的 kb 字符 const string query = ...
- SPOJ - DISUBSTR Distinct Substrings (后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- 牛客多校第一场 B Inergratiion
牛客多校第一场 B Inergratiion 传送门:https://ac.nowcoder.com/acm/contest/881/B 题意: 给你一个 [求值为多少 题解: 根据线代的知识 我们可 ...
- FreeNOS学习2——操作系统是如何启动的
The System Boot Process Explained:https://www.webopedia.com/DidYouKnow/Hardware_Software/BootProcess ...
- 一培训机构设计的学习android课程内容:供大家参考
转自:http://www.cnblogs.com/csj007523/archive/2011/06/16/2082682.html 一培训机构设计的学习android课程内容:供大家参考 第一阶段 ...
- C语言图形界面常用函数集锦
(以下函数均应在图形方式初始之后使用(initgraph(a,b)),在win-tc中使用BGI图形程序模板时,其中已经定义有一个initgr函数,在main函数中应在执行initgr函数之后再使用这 ...
- linux 没有音频输出的解决方式
用户级别的-/.asoundrc 文件. 如果文件不存在,可以手动创建. 其中的各个 ID,请根据实际情况调整: defaults.pcm.card 1 defaults.pcm.device 0 d ...
- MVC 之集合类转化为DataTable
private static DataTable ToDataTableTow(IList list) { DataTable result = new DataTable(); if (list.C ...
- JVM探秘:Java对象
本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. 对象的创建 虚拟机遇到一条 new 指令时,首先去检查这个指令的参数是否能在方法区常量 ...
- Django 链接MySQL及数据操作
Django 链接MySQL Django创建的项目自带的数据库是SQLite3,我们想要链接MySQL的话,需要更改settings.py中的配置 1.在MySQL中创建好数据库,Django项目不 ...