Gym101981I Magic Potion(最大流)
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(最大流)的更多相关文章
- 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 ...
- Magic Potion(最大流,跑两遍网络流或者加一个中转点)
Magic Potion http://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjin ...
- Gym - 101981I The 2018 ICPC Asia Nanjing Regional Contest I.Magic Potion 最大流
题面 题意:n个英雄,m个怪兽,第i个英雄可以打第i个集合里的一个怪兽,一个怪兽可以在多个集合里,有k瓶药水,每个英雄最多喝一次,可以多打一只怪兽,求最多打多少只 n,m,k<=500 题解:显 ...
- 2018icpc南京现场赛-I Magic Potion(最大流)
题意: n个英雄,m个怪兽,第i个英雄可以打第i个集合里的怪兽,一个怪兽可以在多个集合里 有k瓶药水,每个英雄最多喝一次,可以多打一只怪兽,求最多打多少只 n,m,k<=500 思路: 最大流, ...
- hdu4149 Magic Potion
Magic Potion Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- 2018 ACM/ICPC 南京 I题 Magic Potion
题解:最大流板题:增加两个源点,一个汇点.第一个源点到第二个源点连边,权为K,然后第一个源点再连其他点(英雄点)边权各为1,然后英雄和怪物之间按照所给连边(边权为1). 每个怪物连终点,边权为1: 参 ...
- 2018ACM-ICPC亚洲区域赛南京站I题Magic Potion(网络流)
http://codeforces.com/gym/101981/attachments 题意:有n个英雄,m个敌人,k瓶药剂,给出每个英雄可以消灭的敌人的编号.每个英雄只能消灭一个敌人,但每个英雄只 ...
- HDU 4149 Magic Potion
意甲冠军: a[i] ^ x = f[i] ( i = 1...8 ) 和 ( a[1] + a[2] + ... + a[8] ) ^ x = f[9] 如今f为已知 求x 思路: 从低位到高位确 ...
- Magic Potion(网络流)
原题链接 2018南京的铜牌题,听说学长他们上来就A了,我这个图论选手也就上手做了做,结果一言难尽...... 发此篇博客希望自己能牢记自己的菜... 本题大意:有n个heros和m个monsters ...
随机推荐
- 【luogu P3373 线段树2】 模板
题目链接:https://www.luogu.org/problemnew/show/P3373 lazy标记两个,先乘后加 #include <iostream> #include &l ...
- [luoguP3627][APIO2009]抢掠计划
先来看一下题目描述: 题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是,Siru ...
- react setState修改嵌套对象
在react使用setState过程中,难免会遇到复杂的数据类型,,这里还要提醒一下setState 是异步操作行为,需要setState之后的结果做为参数,请求数据的话,可以配合 async aw ...
- Linux CentOS7下安装Zookeeper-3.4.10服务(最新)
Linux CentOS7下安装Zookeeper-3.4.10服务(最新) 2017年10月27日 01:25:26 极速-蜗牛 阅读数:1933 版权声明:本文为博主原创文章,未经博主允许不得 ...
- iOS | FMDB快速上手
任何的开发都或多或少的接触到数据库,而在IOS中一般使用的是SQLite数据库,这是一个轻量功能较为不错的数据库.而现在用到比较多的第三方数据库操作框架就是FMDB.废话不多说,相信查找到这篇文章的都 ...
- 示例浅谈PHP与手机APP开发,即API接口开发
示例浅谈PHP与手机APP开发,即API接口开发 API(Application Programming Interface,应用程序接口)架构,已经成为目前互联网产品开发中常见的软件架构模式,并且诞 ...
- C语言常用关键语法精华总结
1.关于typedef的用法总结 2.typedef struct的用法 3.typedef函数指针用法 4.数组指针(数组类型的指针)与指针数组 5.真正明白c语言二级指针 6.C语言for循环(及 ...
- JavaScript之this解析
1.解析器在调用函数每次都会向函数内部传递进一个隐含的参数,这个隐含的参数就是this,this指向的是一个对象,这个对象我们称为函数执行的上下文对象,根据函数的调用方式不同,this会指向不同的对象 ...
- c语言中 *p++ 和 (*p)++ 和 *(p++) 和 *(++p) 和++(*p)和 *(p--)和 *(--p)有什么区别?
*p++是指下一个地址; (*p)++是指将*p所指的数据的值加一; /******************解释**********************/ ->C编译器认为*和++是同优先级 ...
- Kubernetes-apiserver
Kubernetes API服务器为API对象验证和配置数据,这些对象包含Pod.Service.ReplicationController等等.API Server提供REST操作以及前端到集群的共 ...