zoukankan      html  css  js  c++  java
  • 【 2018南京 】Magic Potion (网络流)

    There are nnn heroes and mmm 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 iii-th hero can only kill one monster belonging to the set MiM_iMi. Joe, the strategist, has kkk 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,kn, m, kn,m,k (1≤n,m,k≤5001 le n, m, k le 5001n,m,k500) — ext{---}— the number of heroes, the number of monsters and the number of bottles of potion.

    Each of the next nnn lines contains one integer tit_iti, the size of MiM_iMi, and the following tit_{i}ti integers Mi,jM_{i, j}Mi,j (1≤j≤ti1 le j le t_i1jti), the indices (111-based) of monsters that can be killed by the iii-th hero (1≤ti≤m,1≤Mi,j≤m1 le t_ile m, 1leq M_{i, j} le m1tim,1Mi,jm).

    Output

    Print the maximum number of monsters that can be killed by the heroes.

    样例输入1

    3 5 2
    4 1 2 3 5
    2 2 5
    2 1 2

    样例输出1

    4

    样例输入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

    样例输出2

    7

    SOLUTION:


    本来的自身流量和额外k个流量是不能混在一起的
    因为有可能 别的hero的自身流量跑到了别的hero身上

    CODE:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int inf=1<<28;
    int cnt=1,head[130000];
    int n,m,s,t;
    inline int Read(){
        int x=0;
        char c=getchar();
        while(c>'9'||c<'0')c=getchar();
        while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
        return x;
    }
    struct Node{
        int v;
        int next;
        int val;
    }node[300010];
    inline void addedge(int u,int v,int val){
        node[++cnt].v=v;
        node[cnt].val=val;
        node[cnt].next=head[u];
        head[u]=cnt;
    }
    int dep[130000],gap[130000];
    void bfs(){
        memset(dep,-1,sizeof(dep));
        memset(gap,0,sizeof(gap));
        dep[t]=0;
        gap[0]=1;
        queue<int>q;
        q.push(t);
        while(!q.empty()){
            int u=q.front();
            q.pop();
            for(int i=head[u];i;i=node[i].next){
                int v=node[i].v;
                if(dep[v]!=-1)continue;
                q.push(v);
                dep[v]=dep[u]+1;
                gap[dep[v]]++;
            }
        }
        return;
    }
    int maxflow;
    int dfs(int u,int flow){
        if(u==t){
            maxflow+=flow;
            return flow;
        }
        int used=0;
        for(int i=head[u];i;i=node[i].next){
            int d=node[i].v;
            if(node[i].val&&dep[d]+1==dep[u]){
                int mi=dfs(d,min(node[i].val,flow-used));
                if(mi){
                    node[i].val-=mi;
                    node[i^1].val+=mi;
                    used+=mi;
                }
                if(used==flow)return used;
            }
        }
        --gap[dep[u]];
        if(gap[dep[u]]==0)dep[s]=n+1;
        dep[u]++;
        gap[dep[u]]++;
        return used;
    }
    int ISAP(){
        maxflow=0;
        bfs();
        while(dep[s]<n)dfs(s,inf);
        return maxflow;
    }
    int k;
    int main(){
    
    
        n=Read(),m=Read(),k=Read();
       for(int  i=1;i<=n;i++)
       {
           int p;cin>>p;
           for(int j=1;j<=p;j++)
           {
               int x;x=Read();
               addedge(i,2*n+x,1);
               addedge(2*n+x,i,0);
               // cout<<i<<" "<<2*n+x<<endl;
           }
       }
    
       for(int i=1;i<=n;i++)
        addedge(n+i,i,2),addedge(i,i+n,0);
    
       for(int i=1;i<=m;i++)
        addedge(2*n+i,2*n+m+i,1),addedge(2*n+m+i,2*n+i,0);
    
       for(int i=1;i<=m;i++)
        addedge(2*n+m+i,2*n+2*m+3,inf),addedge(2*n+2*m+3,2*n+m+i,0);
    
       for(int i=1;i<=n;i++)
        addedge(2*n+2*m+2,n+i,1),addedge(n+i,2*n+2*m+2,0);
    
        for(int i=1;i<=n;i++)
            addedge(2*n+2*m+1,n+i,1),addedge(n+i,2*n+2*m+1,0);
    
       addedge(2*n+2*m+1,2*n+2*m+2,k);
       addedge(2*n+2*m+2,2*n+2*m+1,0);
    
        s=2*n+2*m+1,t=2*n+2*m+3;
        n=2*n+2*m+3;
    
       cout<<ISAP();
    }
    /*
    1 1 4
    1 1
    */
    

      










  • 相关阅读:
    DLL相关注意
    给我个理由要用虚析构函数
    字符指针常量和字符数组
    给自己普及网络基本知识
    面向对象三大基本特性,五大基本原则
    C/C++内存区域划分
    C++多态性
    Java XML Schema详解
    Java SE 第一百零五讲 对象的克隆与浅拷贝
    Java SE 第九十七~一百零五讲 线程与进程
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11231662.html
Copyright © 2011-2022 走看看