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. js获取站点根目录

    function getRootPath(){        var strFullPath=window.document.location.href;        var strPath=win ...

  2. 菜鸟笔记 -- Chapter 6.2.4 成员方法

    6.2.4  成员方法 在Java中使用成员方法对应于类对象的行为,在有些地方也会将方法称之为函数,成员方法是定义在类中具有特定功能的一段独立小程序.方法格式如下: 修饰符 返回值类型 成员方法名 ( ...

  3. 虚拟局域网VLAN的配置实验

    实验涉及命令以及知识补充 交换机的不同状态 switch: :交换机的ROM态 rommon> :路由器的R状态 switch > :用户模式 switch# :特权模式 switch(c ...

  4. c#实现的HTTP服务端

    这次在整理一个服务组件的时候,需要涉及到HTTP的请求,HTTP是应用层,建立在TCP之上的.因此,可以用TCP服务端接收HTTP请求,只需要解析请求内容.HTPP有固定的格式,大家可以直接搜索.网上 ...

  5. #leetcode刷题之路11-盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  6. fjutacm 3700 这是一道数论题 : dijkstra O(mlogn) 二进制分类 O(k) 总复杂度 O(k * m * logn)

    /** problem: http://www.fjutacm.com/Problem.jsp?pid=3700 按二进制将k个待查点分类分别跑dijkstra **/ #include<std ...

  7. ABAP术语-Field

    Field 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/01/1061244.html Name in an ABAP program f ...

  8. Java : JPA相关以及常用注解

    SpringDataJPA自定义的查询方法 定义规范       And 并且 Or 或     Is,Equals 等于 Between 两者之间 LessThan 小于 LessThanEqual ...

  9. ThinkPHP框架目录的介绍

    library目录 Think目录 mvc

  10. scala成长之路(1)基本语法和数据类型

    scala作为JVM上的Lisp,是一种geek类型的编程语言,也一直是我等java程序员眼中的梦寐以求的一门技能,遂下定决心花一段时间好好学习scala.第一天学习,主要介绍与java在编程上的主要 ...