HDU 4292 Food 最大流 + 拆点

很简单的一道网络流题目 由于每个人只能选择一种食物和一种饮料 所以为了限制每个人只选一种 将人拆点 然后就求最大流即可 模板敲错一个小地方 导致无限超时.... 比着模板对了半天才发现哪错了.... 还是对网络流理解的不够深啊....

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

#define REP(i, a, b) for(int i = a; i < b; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define CLR(a, x) memset(a, x, sizeof(a))
#define bug puts("***bug***")

const int maxn = 1000 + 10;
const int INF = 1e9;

struct Edge{
          int u, v, cap, flow;
          Edge(){}
          Edge(int u, int v, int c, int f) : u(u), v(v), cap(c), flow(f){}
};

struct ISAP{
          int n, s, t;
          int d[maxn], p[maxn], cur[maxn], num[maxn];
          vector<int> G[maxn];
          vector<Edge> edges;
          void init(int n){
                    this -> n  = n;
                    REP(i, 0, n) G[i].clear();
                    edges.clear();
          }
          void add(int u, int v, int cap){
                    edges.push_back(Edge(u, v, cap, 0));
                    edges.push_back(Edge(v, u, 0, 0));
                    int m = edges.size();
                    G[u].push_back(m - 2);
                    G[v].push_back(m - 1);
          }
          void bfs(){
                    REP(i, 0, n) d[i] = INF;
                    d[t] = 0;
                    queue<int> Q;
                    Q.push(t);
                    while(!Q.empty()){
                              int x = Q.front(); Q.pop();
                              REP(i, 0, G[x].size()){
                                        Edge &e = edges[G[x][i]];
                                        if(e.cap > 0 || d[e.v] <= d[x] + 1) continue;
                                        d[e.v] = d[x] + 1;
                                        Q.push(e.v);
                              }
                    }
          }
          int augment(){
                    int x = t, a = INF;
                    while(x != s){
                              Edge &e = edges[p[x]];
                              a = min(a, e.cap - e.flow);
                              x = e.u;
                    }
                    x = t;
                    while(x != s){
                              edges[p[x]].flow += a;
                              edges[p[x]^1].flow -= a;
                              x = edges[p[x]].u;
                    }
                    return a;
          }
          int maxflow(int s, int t){
                    this -> s = s; this -> t = t;
                    CLR(cur, 0); CLR(num, 0);
                    bfs();
                    REP(i, 0, n) if(d[i] != INF) num[d[i]]++;
                    int x = s, flow = 0;
                    while(d[s] < n){
                              if(x == t){
                                        flow += augment();
                                        x = s;
                              }
                              int ok = 0;
                              REP(i, cur[x], G[x].size()){
                                        Edge &e = edges[G[x][i]];
                                        if(e.cap > e.flow && d[e.v] + 1 == d[x]){
                                                  ok = 1;
                                                  cur[x] = i;
                                                  p[e.v] = G[x][i];
                                                  x = e.v;
                                                  break;
                                        }
                              }
                              if(!ok){
                                        int m = n - 1;
                                        REP(i, 0, G[x].size()){
                                                  Edge &e = edges[G[x][i]];
                                                  if(e.cap > e.flow) m = min(m, d[e.v]);
                                        }
                                        if(--num[d[x]] == 0) break;
                                        ++num[d[x] = m + 1];
                                        cur[x] = 0;
                                        if(x != s) x = edges[p[x]].u;
                              }
                    }
                    return flow;
          }
}solver;

int n, F, D, x, S, T;

void solve(){
          S = 2 * n + F + D, T = S + 1;
          solver.init(T + 1);
          REP(i, 0, n) solver.add(i * 2, i * 2 + 1, 1);
          REP(i, 0, F){
                    scanf("%d", &x);
                    solver.add(S, n * 2 + i, x);
          }
          REP(i, 0, D){
                    scanf("%d%*c", &x);
                    solver.add(n * 2 + F + i, T, x);
          }
          REP(i, 0, n){
                    REP(j, 0, F){
                              char ch = getchar();
                              if(ch == 'Y') solver.add(n * 2 + j, i * 2, 1);
                    }
                    getchar();
          }
          REP(i, 0, n){
                    REP(j, 0, D){
                              char ch = getchar();
                              if(ch == 'Y') solver.add(i * 2 + 1, n * 2 + F + j, 1);
                    }
                    getchar();
          }
          printf("%d\n", solver.maxflow(S, T));
}

int main()
{
//          freopen("in.txt", "r", stdin);
          while(~scanf("%d%d%d", &n, &F, &D)){
                    solve();
          }
          return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值