2018南京ICPC-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:

在这里插入图片描述

思路:

一个英雄负责一只怪兽,使我想到了二分图最大匹配。那英雄药水怎么解决呢,简单,再匹配一遍,毕竟一位英雄只能用一瓶药水多打一只怪兽,只需稍作修改,还是能用匹配做的。

代码:

#include<bits/stdc++.h>
using namespace std;
struct s{
	int ne;
	int to;
}g[10005];
int head[40005],t=0,ans=0,k=0;
void add(int a,int b){
	g[++t].to=b;
	g[t].ne=head[a];
	head[a]=t;
}
bool Vis[10005];
int Match[10005],match[100005];
int us[10005]={0};
bool Dfs(int u){
    for(int i=head[u];i;i=g[i].ne){
        int v=g[i].to;
        if(Vis[v])  
            continue;
        Vis[v]=true;
        if(!Match[v]||Dfs(Match[v])){
            Match[v]=u;
            Match[u]=v;
            us[v]=1;
            return true;
        }
    }
    return false;
}
bool dfs(int u){
    for(int i=head[u];i;i=g[i].ne){
        int v=g[i].to;
        if(Vis[v])  
            continue;
        Vis[v]=true;
        if(!us[v]||Dfs(Match[v])){
            us[v]=u;
            us[u]=v;
            return true;
        }
    }
    return false;
}
void Solve(int a){
    for(int i=1;i<=a;i++){
        memset(Vis,false,sizeof Vis);
        if(!Match[i])
            if(Dfs(i))
                ans++;
    }
    for(int i=1;i<=a;i++){
        memset(Vis,false,sizeof Vis);
        if(!us[i]&&k>0){
            if(dfs(i)){
				k--;
                ans++;
            }
        }
    }
}

int main(){
int n,m;
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
	int a;
	cin>>a;
	for(int j=1;j<=a;j++){
		int x,y;
		scanf("%d",&x);
		add(n+x,i);
		add(i,n+x);
	}	
}	
Solve(n);
printf("%d",ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值