POJ 3281 Dining (网络流之最大流)
题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料。每头牛都有各自喜欢的食物和饮料,
而每种食物或饮料只能分配给一头牛。最多能有多少头牛可以同时得到喜欢的食物和饮料?
析:是一个经典网络流的题,建立一个超级源点,连向每种食物,建立一个超级汇点,连向每种饮料,然后把每头牛拆成两个点,
一个和食物连,一个和饮料连,最后跑一遍最大流即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 400 + 5;
const int mod = 1e9;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Edge{
int from, to, cap, flow;
}; struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn]; void init(){
edges.clear();
for(int i = 0; i < maxn; ++i) G[i].clear();
} void addEdge(int from, int to, int cap){
edges.push_back((Edge){from, to, cap, 0});
edges.push_back((Edge){to, from, 0, 0});
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
} bool bfs(){
memset(vis, 0, sizeof vis);
queue<int> q;
q.push(s);
d[s] = 0;
vis[s] = 1;
while(!q.empty()){
int x = q.front(); q.pop();
for(int i = 0; i < G[x].size(); ++i){
Edge &e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow){
vis[e.to] = 1;
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
} int dfs(int x, int a){
if(x == t || a == 0) return a;
int flow = 0, f;
for(int &i = cur[x]; i < G[x].size(); ++i){
Edge &e = edges[G[x][i]];
if(d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap-e.flow))) > 0){
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
} int maxFlow(int s, int t){
this->s = s; this->t = t;
int flow = 0;
while(bfs()){
memset(cur, 0, sizeof cur);
flow += dfs(s, INF);
}
return flow;
}
};
Dinic dinic; int main(){
int k;
while(scanf("%d %d %d", &n, &m, &k) == 3){
dinic.init();
int s = 0, t = 402;
for(int i = 1; i <= m; ++i) dinic.addEdge(s, i+200, 1);
for(int i = 1; i <= k; ++i) dinic.addEdge(i+300, t, 1);
for(int i = 1; i <= n; ++i){
int f, d;
dinic.addEdge(i, i+100, 1);
scanf("%d %d", &f, &d);
for(int j = 0; j < f; ++j){
int x;
scanf("%d", &x);
dinic.addEdge(x+200, i, 1);
}
for(int j = 0; j < d; ++j){
int x;
scanf("%d", &x);
dinic.addEdge(i+100, x+300, 1);
}
}
printf("%d\n", dinic.maxFlow(s, t));
}
return 0;
}
POJ 3281 Dining (网络流之最大流)的更多相关文章
- poj 3281 Dining 网络流-最大流-建图的题
题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...
- POJ 3281 Dining 网络流最大流
B - DiningTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...
- POJ 3281 Dining (网络流构图)
[题意]有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和 ...
- poj 3281 Dining 拆点 最大流
题目链接 题意 有\(N\)头牛,\(F\)个食物和\(D\)个饮料.每头牛都有自己偏好的食物和饮料列表. 问该如何分配食物和饮料,使得尽量多的牛能够既获得自己喜欢的食物又获得自己喜欢的饮料. 建图 ...
- POJ 3281 Dining[网络流]
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...
- POJ 3281 Dining(网络流-拆点)
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...
- POJ 3281 Dining (网络流)
POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...
- POJ 3281 Dining(最大流)
POJ 3281 Dining id=3281" target="_blank" style="">题目链接 题意:n个牛.每一个牛有一些喜欢的 ...
- 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)
Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...
随机推荐
- 九度OJ 1162:I Wanna Go Home(我想回家) (最短路径)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:870 解决:415 题目描述: The country is facing a terrible civil war----cities i ...
- Asynchronous_method_invocation 异步方法调用 让步 yielding
zh.wikipedia.org/wiki/同步 [同步不同事件发生 时间一致] 同步(英语:Synchronization),指在一个系统中所发生的事件(event),之间进行协调,在时间上出现一致 ...
- Tomcat学习笔记【5】--- 项目部署详解
本文主要讲在Tomcat中部署项目的几种方式:静态部署.动态部署. 一 静态部署 静态部署项目有好几种方式,比较典型的有如下4种: 1.1 方式一:将Web项目放到webApps目录下 直接将web项 ...
- 【题解】P3129高低卡(白金)High Card Low Card
[题解][P3129 USACO15DEC]高低卡(白金)High Card Low Card (Platinum) 考虑贪心. 枚举在第几局改变规则,在改变规则之前,尽量出比它大的最小的牌,在改变规 ...
- Hibernate load 和 Get的区别
load和get都可以取回一个对象,难道是方法重复吗?绝对不可能,那它们到底有那些区别呢? 在http://blog.chinaunix.net/u/484/showart_1093166.html这 ...
- Android OpenGL 播放视频学习
1, 初步接触Open GL: http://www.cnblogs.com/TerryBlog/archive/2010/07/09/1774475.html 使用GLSurfaceView和Ren ...
- OC中RAC编程block的基本使用
在OC中block的基本使用 // // ViewController.h // RAC--test // // Created by Aaron on 17/1/17. // Copyright © ...
- spring-boot5
Spring Boot集成MyBatis: (1)新建maven project;取名为:spring-boot-mybatis (2)在pom.xml文件中引入相关依赖: (3)创建启动类App.j ...
- 20145239杜文超 《Java程序设计》第8周学习总结
20145239 <Java程序设计>第8周学习总结 教材学习内容总结 通用API 日志API 1.java.util.logging包提供了日志功能相关类与接口,使用日志的起点是logg ...
- Java并发知识概述
1.Java内存模型的抽象结构 Java中,所有的实例.静态域和数组元素都存储在堆内存中,堆内存是线程共享的.局部变量,形参,异常处理参数不会在线程之间共享,所以不存在内存可见性问题,也就不受内存模型 ...