• UVA-10779(最大流)


    题意:

    Bob与他的朋友交换贴纸;他的这些朋友只交换自己没有的贴纸;且用的是自己所有的重复贴纸;现在要求Bob最大能得到多少张贴纸;

    思路:

    把人和物品都进行编号,添加原点s和汇点e,s到每个物品连边容量为Bob拥有的数目;所有物品向汇点e连边容量为1;

    如果一个人向他拥有的物品连边,容量为数目减1,表示他自己会留一个;如果他不拥有某件物品,则物品向这个人连一条边,表示这个人最多接受一件这个物品;

    然后跑一遍最大流就是答案了;

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <bits/stdc++.h>
    #include <stack>
    
    using namespace std;
    
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    
    typedef  long long LL;
    
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=20071027;
    const double PI=acos(-1.0);
    const int inf=1e9;
    const int N=4e5+10;
    const int maxn=500+10;
    const double eps=1e-8;
    
    int n,m,k;
    int cap[40][40],minflow[40],path[40],s,e,num[20],a[40][60];
    queue<int>qu;
    inline void Init()
    {
        s=m+1;e=n+m+1;
        mst(cap,0);
        For(i,1,num[1])
        {
            cap[s][a[1][i]]++;
        }
        For(i,2,n)
        {
            For(j,1,num[i])
            {
                cap[m+i][a[i][j]]++;
            }
            For(j,1,m)
            {
                if(cap[m+i][j])cap[m+i][j]--;
                else cap[j][m+i]=1;
            }
        }
        For(i,1,m)
        {
            cap[i][e]=1;
        }
    }
    
    int bfs()
    {
        mst(path,-1);
        qu.push(s);
        path[s]=0;minflow[s]=inf;
        while(!qu.empty())
        {
            int fr=qu.front();
            qu.pop();
            for(int i=1;i<=e;i++)
            {
                if(path[i]==-1&&cap[fr][i])
                {
                    minflow[i]=min(minflow[fr],cap[fr][i]);
                    path[i]=fr;
                    qu.push(i);
                }
            }
        }
        if(path[e]==-1)return 0;
        return minflow[e];
    }
    
    inline int maxflow()
    {
        int ans=0;
        while(bfs())
        {
            int temp=minflow[e];
            int cur=e;
            while(cur!=s)
            {
                int fa=path[cur];
                cap[fa][cur]-=temp;
                cap[cur][fa]+=temp;
                cur=fa;
            }
            ans+=temp;
        }
        return ans;
    }
    int main()
    {       //freopen("in.txt","r",stdin);
            int t,Case=0;
            read(t);
            while(t--)
            {
                read(n);read(m);
                For(i,1,n)
                {
                    read(num[i]);
                    For(j,1,num[i])read(a[i][j]);
                }
                Init();
                int ans=maxflow();
                printf("Case #%d: %d
    ",++Case,ans);
            }
            
            return 0;
    }
    
     
    

      

  • 相关阅读:
    Odoo13_多选下拉框
    Odoo13_联动下拉框
    Odoo13_上传文件并指定保存到位置
    Flask_cookie和session
    云原生设计理念
    docker 清理掉none对象,未使用网络,缓存等命令
    解决ssh连接中断问题
    python更改程序运行目录为程序所在文件夹
    Vuejs Cron 表达式校验
    Kubernetes WebSocket: Error during WebSocket handshake: Unexpected response code: 403
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5713486.html
Copyright © 2020-2023  润新知