POJ 3281 Dining (网络流之最大流)

摘要:
农夫准备了F(1≤ F≤ 100)食品和D(1≤ D≤ 为他的N(1)杯饮料≤ N≤ 100)头牛。每头牛都有自己喜欢的食物和饮料,每种食物或饮料只能分配给一头牛。有多少头牛能同时得到他们最喜欢的食物和饮料?分析:这是一个经典的网络流量问题。建立一个超级源点,连接到每种食物,建立一个超汇点,连接每种饮料,然后将每头牛分成两个点,一个带食物,一个含饮料,最后运行一次最大流量

题意:农夫为他的 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
", dinic.maxFlow(s, t));
  }
  return 0;
}

免责声明:文章转载自《POJ 3281 Dining (网络流之最大流)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Hibernate的持久化对象配置rsync 服务搭建下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

PCRE函数简介和使用示例【转】

PCRE函数简介和使用示例 标签:正则表达式listbuffercompilationnullperl 原文地址:http://blog.csdn.net/sulliy/article/details/6247155 PCRE是一个NFA正则引擎,不然不能提供完全与Perl一致的正则语法功能。但它同时也实现了DFA,只是满足数学意义上的正则。 P...

winform窗体 小程序【移动窗体和阴影】

窗体无边框设置后无法移动,引用API 使其获得功能 移动 //窗体移动API [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPt...

从零开始配置TypeScript + React + React-Router + Redux + Webpack开发环境

转载请注明出处! 说在前面的话: 1、为什么不使用现成的脚手架?脚手架配置的东西太多太重了,一股脑全塞给你,我只想先用一些我能懂的库和插件,然后慢慢的添加其他的。而且自己从零开始配置也能学到更多的东西不是么。 2、教程只配置了开发环境,并没有配置生产环境。 3、教程针对人群是有过React + Redux经验,并且想在新项目中使用TypeScript的人(...

Git 分支管理最佳实践

it 是目前最流行的源代码管理工具。大量的软件项目由 GitHub、Bitbucket 和 GitLab 这样的云服务平台或是私有的 Git 仓库来管理。在使用 Git 时通常会遇到的一个问题是采用何种分支管理实践,即如何管理仓库中作用不同的各类分支。和软件开发中的其他实践一样,Git 分支管理并没有普遍适用的最佳做法,而只有对每个团队和项目而言最适合的做...

export 和export default的使用和区别

我主要是从vue项目使用常量和方法角度取分析,从具体例子出发 (在js中也可以导入其他js中的数据和方法) 涉及到HelloWord.vue和common.js两个文件 一、使用export导出变量和方法 common.js //导出变量(方式1) const myName = '小明' const myEge = '18'export { myNam...

Qt Meta Object System-元对象系统

研一的时候开始使用Qt,感觉用Qt开发图形界面比MFC的一套框架来方便的多。后来由于项目的需要,也没有再接触Qt了。现在要重新拾起来,于是要从基础学起。 Now,开始学习Qt事件处理机制。 元对象系统的构成 QObject为所有需要利用元对象系统的对象提供一个基类。 Q_OBJECT宏,在类的声明体内激活meta-object功能,比如动态属性、信号和槽...