首页 > 代码库 > POJ 3281 Dining (网络流之最大流)

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 (网络流之最大流)