Problem I. Magic Potion

There are n heroes and m monsters living in an island. The monsters became very vicious these days, so the heroes decided to diminish the monsters in the island. However, the i-th hero can only kill one monster belonging to the set Mi. Joe, the strategist, has k bottles of magic potion, each of which can buff one hero’s power and let him be able to kill one more monster. Since the potion is very powerful, a hero can only take at most one bottle of potion. Please help Joe find out the maximum number of monsters that can be killed by the heroes if he uses the optimal strategy.

Input

  The first line contains three integers n, m, k (1 ≤ n, m, k ≤ 500) — the number of heroes, the number of monsters and the number of bottles of potion. Each of the next n lines contains one integer ti, the size of Mi, and the following ti integers Mi,j (1 ≤ j ≤ ti), the indices (1-based) of monsters that can be killed by the i-th hero (1 ≤ ti ≤ m, 1 ≤ Mi,j ≤ m).

Output

   Print the maximum number of monsters that can be killed by the heroes.

Examples

standard input

3 5 2

4 1 2 3 5

2 2 5

2 1 2

standard output

4

standard input

5 10 2

2 3 10

5 1 3 4 6 10

5 3 4 6 8 9

3 1 9 10

5 1 3 6 7 10

standard output

7

题目大意


给定n个英雄,m只怪兽,k瓶药。每个英雄只能杀一只怪,一个英雄磕了药后能够多杀一只,但一个英雄至多只能磕一次药。已知每个英雄能够杀死哪些怪兽,问最多杀几只怪。

解题思路


添加源点和两个中间结点,编号分别为1,2,3。再添加n个结点表示英雄,编号为4~n+3。再添加m个结点表示怪兽,编号为n+3+1~n+m+3。最后添加汇点,编号为n+m+4。

连接源点和两个中间结点,容量分别为n和k。将每个英雄和两个中间节点连接,容量为1。将每个英雄和他能够消灭的怪兽连接,容量为1。最后将每个怪兽和汇点连接,容量为1。

从源点到汇点跑最大流,结果即为最终的数量。

PS:还有其他解法,如跑一遍容量为1的最大流后,在跑一遍容量为2的最大流,再分类讨论结果 。

AC代码


 #include<bits/stdc++.h>
using namespace std;
const int MAXN = 5e6+; //X 集合中的顶点数上限
const int MAXM = 5e6+; // 总的边数上限
const int INF = 0x3f3f3f3f;
int head[MAXN],tot;
int S,T; // S 是源点,T 是汇点
int d[MAXN]; // 存储每个顶点的层次

struct Edge{
int v,c,nxt;
// v 是指边的另一个顶点,c 表示容量
}e[MAXM];

void init(){
memset(head,-,sizeof(head));
tot=;
}

void addedge(int u,int v,int c){
// 插入一条从 u 连向 v,容量为 c 的弧
e[tot].v=v;e[tot].c=c;
e[tot].nxt=head[u];
head[u]=tot++; e[tot].v=u;e[tot].c=;
e[tot].nxt=head[v];
head[v]=tot++;
}

bool bfs(){
// bfs构建层次图G_L
memset(d,-,sizeof(d));
queue<int> q;
q.push(S);
d[S]=;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].v;
if(e[i].c>&&d[v]==-){
q.push(v);
d[v]=d[u]+;
}
}
}
return (d[T]!=-);
}

int dfs(int u,int flow){
// dfs在层次图G_L中寻找增广路径
// flow 表示当前搜索分支的流量上限
if(u==T){
return flow;
}
int res=;
for(int i=head[u];i!=-;i=e[i].nxt){
int v=e[i].v;
if(e[i].c>&&d[u]+==d[v]){
int tmp=dfs(v,min(flow,e[i].c));
// 递归计算顶点 v,用 c(u, v) 来更新当前流量上限
flow-=tmp;
e[i].c-=tmp;
res+=tmp;
e[i^].c+=tmp; // 修改反向弧的容量
if(flow==){ // 流量达到上限,不必继续搜索了
break;
}
}
}
if(res==){
// 当前没有经过顶点 u 的可行流,不再搜索顶点 u
d[u]=-;
}
return res;
}

int maxflow(){ // 函数返回值就是最大流的结果
int res=;
while(bfs()){
res+=dfs(S,INF); // 初始流量上限为 INF
}
return res;
}

int main(){
int m,n,k,mid1,mid2;
while(~scanf("%d %d %d",&n,&m,&k)){
init();
S=;T=n+m+;mid1=;mid2=;
int cnt,v;
addedge(S,mid1,n);
addedge(S,mid2,k);
for(int i=;i<=n;i++){
addedge(mid1,i+,);
addedge(mid2,i+,);
scanf("%d",&cnt);
for(int j=;j<cnt;j++){
scanf("%d",&v);
addedge(i+,n+v+,);
}
}
for(int i=;i<=m;i++)
addedge(n+i+,T,);

printf("%d\n",maxflow());
}
return ;
}
 

Gym101981I Magic Potion(最大流)的更多相关文章

  1. Gym 101981I - Magic Potion - [最大流][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem I]

    题目链接:http://codeforces.com/gym/101981/attachments There are n heroes and m monsters living in an isl ...

  2. Magic Potion(最大流,跑两遍网络流或者加一个中转点)

    Magic Potion http://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjin ...

  3. Gym - 101981I The 2018 ICPC Asia Nanjing Regional Contest I.Magic Potion 最大流

    题面 题意:n个英雄,m个怪兽,第i个英雄可以打第i个集合里的一个怪兽,一个怪兽可以在多个集合里,有k瓶药水,每个英雄最多喝一次,可以多打一只怪兽,求最多打多少只 n,m,k<=500 题解:显 ...

  4. 2018icpc南京现场赛-I Magic Potion(最大流)

    题意: n个英雄,m个怪兽,第i个英雄可以打第i个集合里的怪兽,一个怪兽可以在多个集合里 有k瓶药水,每个英雄最多喝一次,可以多打一只怪兽,求最多打多少只 n,m,k<=500 思路: 最大流, ...

  5. hdu4149 Magic Potion

    Magic Potion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  6. 2018 ACM/ICPC 南京 I题 Magic Potion

    题解:最大流板题:增加两个源点,一个汇点.第一个源点到第二个源点连边,权为K,然后第一个源点再连其他点(英雄点)边权各为1,然后英雄和怪物之间按照所给连边(边权为1). 每个怪物连终点,边权为1: 参 ...

  7. 2018ACM-ICPC亚洲区域赛南京站I题Magic Potion(网络流)

    http://codeforces.com/gym/101981/attachments 题意:有n个英雄,m个敌人,k瓶药剂,给出每个英雄可以消灭的敌人的编号.每个英雄只能消灭一个敌人,但每个英雄只 ...

  8. HDU 4149 Magic Potion

    意甲冠军: a[i] ^ x = f[i] ( i = 1...8 ) 和 ( a[1] + a[2] + ... + a[8] ) ^ x = f[9] 如今f为已知  求x 思路: 从低位到高位确 ...

  9. Magic Potion(网络流)

    原题链接 2018南京的铜牌题,听说学长他们上来就A了,我这个图论选手也就上手做了做,结果一言难尽...... 发此篇博客希望自己能牢记自己的菜... 本题大意:有n个heros和m个monsters ...

随机推荐

  1. 学大伟业 Day 5 培训总结

    今天讲数据结构 先从mzx大佬的ppt摘抄一段: 数据结构是计算机存储.组织数据的方式.数据结构是指相互之间存在一种或多种特定关系的数据元素的集合. 通常情况下,精心选择的数据结构可以带来更高的运行或 ...

  2. Android学习笔记_30_常用控件使用

    一.状态栏通知(Notification): 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息.发送消息的代码如下: public void sendNotice(View v){ int ic ...

  3. centos修改hostname

    1.临时修改 hostname localhost 2.永久修改 vim /etc/sysconfig/network 修改hostname的值后保存

  4. Mysql之inner join,left join,right join详解

    首先借用官方的解释下: inner join(等值连接):只返回两个表中联结字段相等的行: left join(左联接):返回包括左表中的所有记录和右表中联结字段相等的记录: right join(右 ...

  5. PHP中级面试经历

    资源来自网络:http://www.2cto.com/kf/201304/204749.html 1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分) 2.echo(),p ...

  6. MarkDown/Html在线转换(支持代码高亮,可复制到微信公众号、今日头条)

    MarkDown/Html在线转换能够将md渲染成html并且能保持代码高亮,可以方便的复制待格式的html粘贴到微信公众号,CSDN,简书,博客园,开源中国等. 扫码体验在线助手小程序 我是java ...

  7. css的基础用法(上)

    css定义: CSS层叠式样表(Cascading  Style Sheets)是一种用来表现html或xml等文件样式的计算机语言.CSS不仅可以静态的修饰网页,还可以配合各种脚本语言动态地对网页个 ...

  8. BFC、IFC、GFC、FFC

    原文地址:https://www.xingkongbj.com/blog/css/fc.html FC FC的全称是:Formatting Contexts,是W3C CSS2.1规范中的一个概念.它 ...

  9. 使用RMAN对数据文件进行恢复

    (1)备份数据库 在使用RMAN进行数据库恢复之前,先用RMAN进行全库备份 [oracle@redhat6 ~]$ rman target / Recovery Manager: Release : ...

  10. npm ERR! code: 'EPERM' (权限问题 errro permit)

    PS C:\Users\user\Desktop\test\my-project> npm run iview --save npm ERR! missing script: iview npm ...