Gym - 101981I-MagicPotion-最大流

题目描述

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.
Sample Input
sample input 1
3 5 2
4 1 2 3 5
2 2 5
2 1 2
sample input 2
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
Sample Output
sample output 1
4
sample output 2
7

题目分析

发现是匹配问题,思考将问题建图后用最大流算法进行解决。

如果没有k个药水,则很明确,将问题转化成一个二部图,从源点到英雄,从英雄到怪兽,从怪兽到汇点,路径上的容量都为1,则最大流就是杀死的最多的怪兽。

因为还有k个药水,并且每一个英雄都只能拿一个药水,我们从源点引出一个点,容量为k,再从这个点到每个英雄引一条路径,每条路径容量为1

AC代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<climits>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<cmath>

using namespace std;

typedef long long ll;
const int MAXN=1020;
const int INF=0x3f3f3f3f;

struct Edge
{
	int from,to,cap,flow;
	Edge(int _from,int _to,int _cap,int _flow):from(_from),to(_to),cap(_cap),flow(_flow){}
};
struct Dinic
{
	int s,t;
	vector<Edge> edges;
	vector<int> G[MAXN];
	int d[MAXN]; bool vis[MAXN]; int cur[MAXN];
	void addEdge(int from,int to,int cap)
	{
		edges.push_back((Edge){from,to,cap,0});
		edges.push_back((Edge){to,from,0,0});
		int tt=edges.size();
		G[from].push_back(tt-2);
		G[to].push_back(tt-1);
	}
	bool BFS()
	{
		memset(vis,0,sizeof(vis));
		queue<int>Q;
		Q.push(s);
		d[s]=0;
		vis[s]=1;
		while(!Q.empty())
		{
			int x=Q.front(); Q.pop();
			for(int i=0;i<G[x].size();++i)
			{
				Edge &e =edges[G[x][i]];
				if(!vis[e.to] && e.cap>e.flow)
				{
					vis[e.to]=1;
					d[e.to]=d[x]+1;
					Q.push(e.to);
				}
			}
		}
		return vis[t];
	}
	int DFS(int x,int a)
	{
		if(x==t || a==0) return a;
		int flow=0,f;
		for(int&i=cur[x];i<G[x].size();++i)
		{
			Edge& e=edges[G[x][i]];
			if(d[x]+1==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0)
			{
				e.flow+=f;
				edges[G[x][i]^1].flow-=f;
				flow+=f;
				a-=f;
				if(a==0) break;
			}
		}
		return flow;
	}
	int MF(int s,int t)
	{
		this->s=s; this->t=t;
		int flow=0;
		while(BFS())
		{
			memset(cur,0,sizeof(cur));
			flow+=DFS(s,INF);
		}
		return flow;
	}
}dinic;

int n,m,k;

int main()
{
	scanf("%d%d%d",&n,&m,&k);
	int tmp=n+m+1;
	int t=tmp+1;
	int s=0;
	dinic.addEdge(s,tmp,k);
	for(int i=1;i<=n;i++)
	{
		dinic.addEdge(s,i,1); dinic.addEdge(tmp,i,1);
		int t,u; scanf("%d",&t);
		while(t--)
		{
			scanf("%d",&u); dinic.addEdge(i,u+n,1);
		}
	}
	for(int i=1;i<=m;i++)
	{
		dinic.addEdge(i+n,t,1);
	}
	printf("%d\n",dinic.MF(s,t));
	return 0;
}
经验总结

第一次写网络流的题目。感觉就是在使用工具,如何正确地使用才是问题的关键,而不一定要求掌握工具怎么造出来的。这对当下我来说很重要。当然掌握工具的原理以后就能更好的使用工具。后面会花功夫具体去学习网络流的原理的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值