Problem I. Magic Potion
Input file: standard input
Output file: standard output
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 standard output
3 5 2
4 1 2 3 5
2 2 5
2 1 2
4
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
题意,n个凹凸曼,m个小怪兽,k瓶药水,每个凹凸曼能打败对应的怪兽,不过每个凹凸曼只能打死一个怪兽,但是如果凹凸曼喝了药水后就可以再打死一个,但是一个凹凸曼只能喝一瓶药水,问最多能打死几只小guaishou
思路:二分图最大匹配,匹配两次,第一次是没和药水,第二次是喝药水
#include
#include
#include
#include
using namespace std;
const int MaxN = 505;
int n,m,k;
vector G[MaxN];
bool used[MaxN];
int Size[MaxN],from[MaxN];
bool match(int x){
for(int i = 0;i < Size[x]; i++){
if(!used[G[x][i]]){
used[G[x][i]] = 1;
if(from[G[x][i]] == -1 || match(from[G[x][i]])){
from[G[x][i]] = x;
return 1;
}
}
}
return 0;
}
int hungry(){
int tot = 0;
for(int i = 1;i <= n; i++) Size[i] = G[i].size();
for(int i = 1;i <= n; i++){
for(int j = 1;j <= m; j++) used[j] = 0;
if(match(i)) ++tot;
}
return tot;
}
int main()
{
scanf("%d %d %d",&n,&m,&k);
for(int i = 1;i <= n; i++){
int q;
scanf("%d",&q);
for(int j = 1;j <= q; j++){
int cur;
scanf("%d",&cur);
G[i].push_back(cur);
}
}
for(int i = 1;i <= m; i++) from[i] = -1;
int Q = hungry();
int QQ = hungry();
printf("%d\n", Q + min(QQ, k));
}