题目: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);
}