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. 封装方法到对象(javascript)

    /*! * artDialog 5 * Date: 2012-03-21 * http://code.google.com/p/artdialog/ * (c) 2009-2012 TangBin, ...

  2. Android学习笔记_16_添加多个Activity、参数传递、请求码和结果码使用

    一.添加新的Activity步骤: 第一步:新建一个继承Activity的类,如:NewActivity public class NewActivity extends Activity { @Ov ...

  3. FileUpload框架实现文件上传(多个文件)和下载

    一.文件上传: 对于大文件执行下面的代码之间卡死 package com.example.web.service.servlet; import java.io.File; import java.i ...

  4. Zookeeper入门开发demo

    package CreateGroup; import java.io.IOException; import java.util.List; import java.util.concurrent. ...

  5. EF执行SQL语句

    使用上下文中的Database.SqlQuery<对应的表名>(sql语句) var data = dbcenter.Database.SqlQuery<CcBusiFormview ...

  6. C++的抽象类、虚函数、虚基类和java的抽象类和接口

    简单整理如下: C++虚函数 == java普通函数 C++纯虚函数 == java抽象函数 C++抽象类 == java抽象类 C++虚基类(全都是纯虚函数) == java接口

  7. bzoj3895: 取石子(博弈论,记忆化搜索)

    3895: 取石子 Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 361  Solved: 177[Submit][Status][Discuss] D ...

  8. 【PTA 天梯赛训练】修理牧场(哈夫曼树+优先队列)

    农夫要修理牧场的一段栅栏,他测量了栅栏,发现需要N块木头,每块木头长度为整数L​i​​个长度单位,于是他购买了一条很长的.能锯成N块的木头,即该木头的长度是L​i​​的总和. 但是农夫自己没有锯子,请 ...

  9. Spirng+SpringMVC+Mybatis(一)

    实习之后都是在别人搭配好环境的情况下进行一些业务的编写,脑袋已经不记得如何搭建一个ssm项目的,所以周末有空补了一下. 首先新建一个test数据库,并且在里面插入三条数据.如图下 编写一个User B ...

  10. Linux 必会

    一.一般命令:1.cd 进入磁盘文件夹2.ls- 查看当前文件夹包含哪些文件,注意-后面的3.pwd 立刻知道目前所在哪个文件及4.mkdir 创建文件夹5.touch touch命令用于修改文件或者 ...