Gym 101981I 最大流

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.

题意:有n个英雄,m只怪兽,第i个英雄只能在怪兽集Mi中选取一个怪兽消灭,但是有k个buff,每个buff能够让一个英雄能够多消灭一只怪兽,但一个英雄只能获得一次buff,问最多能有多少只怪兽被消灭
思路:首先设一个源点s,汇点t,源点向每个英雄连一条容量为1的边,再向buff连一条容量为k的边,buff再向每个英雄连一条容量为1的边,即可保证正常情况下一个英雄只能去消灭1只怪兽,但有k个buff,每个英雄也只能得到1个buff,,然后每个英雄向对应 怪兽集 中的每一个怪兽连一条容量为1的边,保证一只怪兽只会被消灭一次,每个怪兽又向汇点连一条容量为1的边,跑最大流即可

#include<bits/stdc++.h>
#define MAXN 1010
#define MAXM 251510
using namespace std;
const int INF = 0x3f3f3f3f;
int head[MAXN],tot;
struct edge
{
    int v,c,nxt;
}edg[MAXM << 1];
inline void addedg(int u,int v,int c)
{
    edg[tot].v = v;
    edg[tot].c = c;
    edg[tot].nxt = head[u];
    head[u] = tot++;
}
inline void add(int u,int v,int c)
{
    addedg(u,v,c);
    addedg(v,u,0);
}
int n,m,k,d[MAXN];
inline bool bfs(int s,int t)
{
    queue<int> qu;
    memset(d,-1,sizeof(int)*(n+m+3));
    qu.push(s);
    d[s] = 0;
    int v;
    while(!qu.empty())
    {
        int u = qu.front();
        qu.pop();
        for(int i = head[u];i != -1;i = edg[i].nxt)
        {
            v = edg[i].v;
            if(edg[i].c > 0 && d[v] == -1)
                d[v] = d[u]+1,qu.push(v);
        }
    }
    return d[t] != -1;
}
int dfs(int u,int flow,int t)
{
    if(u == t)
        return flow;
    int res = 0;
    for(int i = head[u];i != -1;i = edg[i].nxt)
    {
        int v = edg[i].v;
        if(edg[i].c > 0 && d[v] == d[u] + 1)
        {
            int tmp = dfs(v,min(flow,edg[i].c),t);
            flow -= tmp;
            res += tmp;
            edg[i].c -= tmp;
            edg[i^1].c += tmp;
            if(flow == 0)
                break;
        }
    }
    if(res == 0)
        d[u] = -1;
    return res;
}
inline void init()
{
    memset(head,-1,sizeof(int)*(n+m+3));
    tot = 0;
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        init();
        int s,t;
        s = 0,t = n+m+2;
        for(int i = 1;i <= n;++i)
            add(s,i,1),add(n+1,i,1);
        add(s,n+1,k);
        for(int i = n+2;i <=n+1+m;++i)
            add(i,t,1);
        int len,v;
        for(int i = 1;i <= n;++i)
        {
            scanf("%d",&len);
            while(len--)
            {
                scanf("%d",&v);
                add(i,v+n+1,1);
            }
        }
        int ans = 0;
        while(bfs(s,t))
            ans += dfs(s,INF,t);
        printf("%d\n",ans);
    }
    return 0;
}
发布了50 篇原创文章 · 获赞 3 · 访问量 3114

猜你喜欢

转载自blog.csdn.net/xing_mo/article/details/103980149