这个题是很难往网络流上面构思的。。。

从s向每个物品增加容量为Bob拥有数的弧,然后从每个物品向t增加容量为1的弧(代表种类个数)。这时候跑最大流的话,得到的肯定是Bob拥有的初始种类数。那么交换后的最大数呢?

对于Bob以外的小伙伴,如果i拥有j物品超过1个(交换后他自己至少保留一个),从人节点i向物品节点j增加容量为num-1的弧,表示他能输出多少物品,而如果i没有j物品,那么从物品节点j向人节点i增加容量为1的弧(他最多接受1单位的物品)。然后跑最大流得到的就是答案了。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<fstream>
#include<sstream>
#include<bitset>
#include<vector>
#include<string>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
using namespace std; const int maxn = 300;
const int INF = 1e9; int n, m, s, t, num[11][30];
int d[maxn], cur[maxn];
bool vis[maxn]; struct Edge
{
int from, to, cap, flow;
};
vector<Edge> edges;
vector<int> G[maxn]; void init()
{
s = 0, t = n + m + 1; CLR(num, 0);
REP(i, t+1) G[i].clear(); edges.clear();
} void add(int from, int to, int cap)
{
edges.PB((Edge){from, to, cap, 0});
edges.PB((Edge){to, from, 0, 0});
int nc = edges.size();
G[from].PB(nc-2); G[to].PB(nc-1);
} bool bfs()
{
CLR(vis, 0);
queue<int> q; q.push(s);
d[s] = 0, vis[s] = 1;
while(!q.empty())
{
int x = q.front(); q.pop();
int nc = G[x].size();
REP(i, nc)
{
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, nc = G[x].size();
for(int& i = cur[x]; i<nc; 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 max_flow()
{
int flow = 0;
while(bfs())
{
CLR(cur, 0);
flow += dfs(s, INF);
}
return flow;
} int main()
{
int T; scanf("%d", &T);
FF(kase, 1, T+1)
{
scanf("%d%d", &n, &m);
init();
int x;
REP(i, n)
{
scanf("%d", &num[i][0]);
while(num[i][0]--)
{
scanf("%d", &x);
num[i][x]++;
}
}
FF(i, 1, m+1)
{
if(num[0][i]) add(s, i+n, num[0][i]);
add(i+n, t, 1);
}
FF(i, 1, n)
{
FF(j, 1, m+1)
{
if(num[i][j] > 1) add(i, j+n, num[i][j] - 1);
if(num[i][j] == 0) add(j+n, i, 1);
}
}
printf("Case #%d: %d\n", kase, max_flow());
}
return 0;
}

UVA 10779 Collectors Problem(最大流)的更多相关文章

  1. uva 10779 Collectors Problem 网络流

    链接 一共有n个人, m种收藏品, 每个人拥有的收藏品的种类和个数都是不相同的. 假设2-n这些人都只和1互相交换, 比例是1:1, 并且, 2-n这些人, 只换自己现在没有的, 如果他现在有第二种, ...

  2. AC日记——Collectors Problem uva 10779

    UVA - 10779 思路: 最大流: s向所有的贴纸的种类连边,流量为Bob拥有的数量: 然后,Bob的朋友如果没有这种贴纸,则这种贴纸向bob的朋友连边,容量1: 如果bob的朋友的贴纸很多大于 ...

  3. UVA 10779 (最大流)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33631 题目大意:Bob有一些贴纸,他可以和别人交换,他可以把自己 ...

  4. uva 11991 - Easy Problem from Rujia Liu?(STL)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=3142" target="_blank ...

  5. CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu?

    CJOJ 2485 UVa 11991 生日礼物 / UVa 11991 Easy Problem from Rujia Liu? Description (原题来自刘汝佳<训练指南>Pa ...

  6. UVa10779 Collectors Problem(最大流)

    很容易想到源点向所类型有贴纸连边,容量为Bob一开始有的数量:然后贴纸向汇点连边,容量为1. 接下来就是交换部分的连边了.注意交换一次一次进行,每次只能交换一张. 交换,是对于两种贴纸而言,仅会发生在 ...

  7. Risk UVA - 12264 拆点法+最大流+二分 最少流量的节点流量尽量多。

    /** 题目:Risk UVA - 12264 链接:https://vjudge.net/problem/UVA-12264 题意:给n个点的无权无向图(n<=100),每个点有一个非负数ai ...

  8. UVA 820 --- POJ 1273 最大流

    找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的 ...

  9. UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)

    Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...

随机推荐

  1. html--offsetLeft,Left,clientLeft的关键--动态获取计算元素位置关系

    动态计算元素位置关系的时候,必备... http://www.cnblogs.com/panjun-Donet/articles/1294033.html

  2. 提供几个可注册的edu邮箱链接

    旧版的邮箱大全有edu邮箱的专题页面,放出来2个国内edu.cn邮箱的注册地址:@live.shop.edu.cn和@abc.shop.edu.cn,现在已经停止开放注册了. 其实旧版中还做了个隐藏的 ...

  3. java编写二叉树以及前序遍历、中序遍历和后序遍历 .

    /** * 实现二叉树的创建.前序遍历.中序遍历和后序遍历 **/ package DataStructure; /** * Copyright 2014 by Ruiqin Sun * All ri ...

  4. P51、面试题5:从尾到头打印链表

    题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值. 链表结点定义如下: Struct ListNode{ int   m_nKey; ListNode*   m_pNext; }; 我们可 ...

  5. size_t和ssize_t

    Ssize_t 与size_t 跟踪linux源码得到以下宏: #ifndef _SIZE_T #define _SIZE_T typedef __kernel_size_t         size ...

  6. python的常用概念

    常用的概念 主体字符串 主体列表 内置函数和方法的区别 映射表 引用 迭代器: 1. 字典:单步遍历迭代器 2. 文件:逐行读取的迭代器

  7. CCS使用TIPS

    2013-06-20 09:37:49 CCS使用TIPS: 代码编写: CCS中通过Using CodeSense方便写代码,跟VC助手类似,具体使用方法在ccs的help中搜索using visu ...

  8. [ffmpeg 扩展第三方库编译系列] 关于libvpx mingw32编译问题

    在编译libvpx的时候遇到挺多的问题, 1.[STRIP] libvpx.a < libvpx_g.a strip: Bad file number   这个错误也是比较难搞的,一开始以为只是 ...

  9. Task-based Asynchronous Pattern (TAP)

    The Task-based Asynchronous Pattern (TAP) is based on the System.Threading.Tasks.Task and System.Thr ...

  10. Hive简介

    实验简介 我们本节课程主要介绍 Hive 的相关知识,将会涉及以下内容: Hive 的定义 Hive 的体系结构 Hive 与关系数据库的区别 Hive 的应用场景 Hive 的存储 一.什么是 Hi ...