UVA 10779 Collectors Problem 最大流

题意:Bob手里有一些贴纸 他想和他的朋友们交换贴纸 使得自己的贴纸种类最多 Bob的朋友们相互之间不能交换 只能和Bob换 且只拿自己有重复的贴纸交换自己没有的贴纸

思路:把Bob当作源点 把他的朋友们以及贴纸的种类看成不同的点 将Bob与所拥有的贴纸种类连边 容量为该种贴纸Bob拥有的数量 对于他的一个朋友i 如果他的一种贴纸数量超过1 就将i与该种贴纸种类连边 容量为他有的该种贴纸数量减一 代表i最多可以转让这么多这种贴纸 自己还要留一张 对于自己没有的贴纸种类 把这种贴纸种类和i连边 容量为1 代表i接受一张该种类的贴纸  最后所有种类贴纸和汇点连边 求最大流即可

还是自己太弱了 想了半天不会建图 看了题解... 的确 网络流的核心是建图 而这正是我所欠缺的 建图的方式千变万化 没有大量的网络流训练是不行的 我会坚持刷下去 加油加油!!

#include <cstdio>
#include <cstring>
#include <algorithm>
#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 CPY( a, x ) memcpy( a, x, sizeof a )

const int maxn = 50 + 10;
const int maxe = 2000 + 10;
const int INF = 1e9;

struct Edge{
          int v, c, f;
          int next;
          Edge() {}
          Edge(int v, int c, int f, int next) : v(v), c(c), f(f), next(next) {}
};

struct ISAP{
          int n, s, t;
          int num[maxn], cur[maxn], d[maxn], p[maxn];
          int Head[maxn], cntE;
          int Q[maxn], head, tail;
          int cut[maxe], cutE, vis[maxn];
          Edge edge[maxe];
          void Init(int n){
                    this -> n = n;
                    cntE = cutE = 0;
                    CLR(Head, -1);
                    CLR(vis, 0);
          }
          void Add(int u, int v, int c){
                    edge[cntE] = Edge(v, c, 0, Head[u]);
                    Head[u] = cntE++;
                    edge[cntE] = Edge(u, 0, 0, Head[v]);
                    Head[v] = cntE++;
          }
          void Bfs(){
                    CLR(d, -1);
                    CLR(num, 0);
                    d[t] = 0;
                    head = tail = 0;
                    Q[tail++] = t;
                    num[0] = 1;
                    while(head != tail){
                              int u = Q[head++];
                              for(int i = Head[u]; ~i; i = edge[i].next){
                                        Edge &e = edge[i];
                                        if(~d[e.v]) continue;
                                        d[e.v] = d[u] + 1;
                                        Q[tail++] = e.v;
                                        num[d[e.v]] ++;
                              }
                    }
          }
          int Maxflow(int s, int t){
                    this -> s = s;
                    this -> t = t;
                    CPY(cur, Head);
                    Bfs();
                    int flow = 0, u = p[s] = s;
                    while(d[s] < n){
                              if(u == t){
                                        int f = INF, neck;
                                        for(int i = s; i != t; i = edge[cur[i]].v){
                                                  if(f > edge[cur[i]].c - edge[cur[i]].f){
                                                            f = edge[cur[i]].c - edge[cur[i]].f;
                                                            neck = i;
                                                  }
                                        }
                                        for(int i = s; i != t; i = edge[cur[i]].v){
                                                  edge[cur[i]].f += f;
                                                  edge[cur[i]^1].f -= f;
                                        }
                                        flow += f;
                                        u = neck;
                              }
                              int ok = 0;
                              for(int i = cur[u]; ~i; i = edge[i].next){
                                        Edge &e = edge[i];
                                        if(e.c > e.f && d[e.v] + 1 == d[u]){
                                                  ok = 1;
                                                  cur[u] = i;
                                                  p[e.v] = u;
                                                  u = e.v;
                                                  break;
                                        }
                              }
                              if(!ok){
                                        int m = n - 1;
                                        if(--num[d[u]] == 0) break;
                                        for(int i = Head[u]; ~i; i = edge[i].next){
                                                  Edge &e = edge[i];
                                                  if(e.c - e.f > 0 && m > d[e.v]){
                                                            cur[u] = i;
                                                            m = d[e.v];
                                                  }
                                        }
                                        ++num[d[u] = m + 1];
                                        u = p[u];
                              }
                    }
                    return flow;
          }

}solver;

int n, m, cas = 0;
int mes[15][30];

void input(){
          int x, u;
          CLR(mes, 0);
          scanf("%d%d", &n, &m);
          solver.Init(n + m + 2);
          FOR(i, 1, n){
                    scanf("%d", &x);
                    while(x--){
                              scanf("%d", &u);
                              mes[i][u]++;
                    }
          }
}

void solve(){
          int S = 1, T = n + m + 1;
          FOR(i, 1, m) if(mes[1][i]) solver.Add(S, i + n, mes[1][i]);
          FOR(i, 2, n) FOR(j, 1, m){
                    if(mes[i][j] > 1) solver.Add(i, j + n, mes[i][j] - 1);
                    else if(!mes[i][j]) solver.Add(j + n, i, 1);
          }
          FOR(i, 1, m) solver.Add(i + n, T, 1);
          int ans = solver.Maxflow(S, T);
          printf("Case #%d: %d\n", ++cas, ans);
}

int main()
{
          //freopen("in.txt", "r", stdin);
          int T;
          scanf("%d", &T);
          while(T--){
                    input();
                    solve();
          }
          return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值