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 ...
随机推荐
- 前端静态文件如何应对HTTPS的到来
近几年,越来越多的网站开始支持https,我们可以看到国外的比如github.谷歌.facebook:国内的有百度.淘宝.博客园.coding.net.worktile等一系列的网站. 我再最近的开发 ...
- data-ng-click 指令
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- js中实现页面跳转(返回前一页、后一页)
一:JS 重载页面,本地刷新,返回上一页 代码如下: <a href="javascript:history.go(-1)">返回上一页</a> <a ...
- 一站式学习Redis 从入门到高可用分布式实践(慕课)第六章 Redis开发运维常见问题
fork操作 1.同步操作 2.与内存量息息相关:内存越大,耗时越长(与机器类型有关) 3.info:latest_fork_usec 进程外开销 AOF追加阻塞 不知道哪个命令??? 单机多实例部署 ...
- poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点
/** problem: http://poj.org/problem?id=2553 将所有出度为0环中的点排序输出即可. **/ #include<stdio.h> #include& ...
- InheritableThreadLocal线程复用
引自:http://www.cnblogs.com/sweetchildomine/p/6575666.html 虽然使用AOP可以获取方法签名,但是如果要获取方法中计算得出的数据,那么就得使用Thr ...
- mysql创建表时反引号的作用
试用navicat工具查看现网mysql建表语句时,发现表名和字段名都是反引号引起来的 CREATE TABLE `tab_notice_title_tv` ( `i_id` ) NOT NULL A ...
- 【cisco探索之路】
CISCO探索之路 show&debug&clear 1:show show version:显示版本信息show running-config:显示当前的配置show interfa ...
- Hadoop(8)-HDFS的读写数据流程以及机架感知
1. HDFS的写数据流程 1.客户端通过fs模块向NameNode申请文件上传,NameNode检查请求是否合法,如用户权限,目标文件是否已存在,父目录是否存在等等 2.NameNode返回是否可以 ...
- hive 学习系列二(数据库的创建删除修改) 拿走,不谢。
database 相当于一个目录或者命名空间,用来更好地进行表的管理 在hdfs 的目录位置大致如下: [root@iZbp12vtv76y9q3d633bh6Z /]# hadoop fs -ls ...