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 ...
随机推荐
- 基于传统IPC基础上的RTMP互联网推流摄像机方案设计
在我之前的一篇博客<EasyRTMP内置进入摄像机中实现网络推流直播摄像机的功能>中,我阐述了一种将RTMP推流内置到摄像机系统内部,实现安防摄像机转互联网直播的RTMP推流摄像机功能,如 ...
- 常见 WEB 安全漏洞(转)
SQL注入 成因:程序未对用户的输入的内容进行过滤,从而直接代入数据库查询,所以导致了sql 注入 漏洞 . 思路:在URL处可以通过 单引号 和 and 1=1 and 1=2 等语句进行手工测试s ...
- 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...
- Django框架ORM单表删除表记录_模型层
此方法依赖的表是之前创建的过的一张表 参考链接:https://www.cnblogs.com/apollo1616/p/9840354.html 1.删除方法就是delete(),它运行时立即删除对 ...
- [2018-10-17]宁波dotnet社区(NBDNC)第一次问卷关于dotnet技术栈的小调查
最近(2018年10月7日至10月17日),为配合确定下一次社区线下活动主题,做了一次宁波dotnet社区(NBDNC)的本地dotnet技术栈调研,设计了一份问卷,在此做一次记录. 导出的问卷统计结 ...
- Django--组件-用户认证Auth(auth_user增加字段)
引入 : from django.db import models from django.contrib.auth.models import AbstractBaseUser 源码 : fro ...
- ZOJ - 1504 Slots of Fun 【数学】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1504 题意 给出一串字符串 里面的每个字符的位置 都按照题目的意 ...
- linux 下监控进程流量情况命令 NetHogs
摘自: http://www.cnblogs.com/kerrycode/p/4748970.html NetHogs介绍 NetHogs是一款开源.免费的,终端下的网络流量监控工具,它可监控Linu ...
- 关于for 循环里 线程执行顺序问题
最近在做项目时遇到了 这样的需求 要在一个for循环里执行下载的操作, 而且要等 下载完每个 再去接着走循环.上网查了一些 觉得说的不是很明确.现在把我用到的代码 贴上 希望可以帮到有此需求的开发者 ...
- poj1417 True Liars[并查集+背包]
有一点小转化的题,在设计dp状态时还是有点费脑筋的. 地址. 依题意,首先可以知道肯定要扩展域的并查集(明摆着的嘛).一个"好人"域,一个"坏人"域,每句话分两 ...