HihoCoder 1393 二分图多重匹配 Dinic

#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<stack>
#include<set>
#include<bitset>
using namespace std;

const int MAX=205;
const int INF=0x3f3f3f3f;

int Dis[MAX][MAX],Layer[MAX],SSS,TTT;
bool Visited[MAX];
int F[MAX];

bool CountLayer()
{
    deque<int> que;
    memset(Layer,-1,sizeof(Layer));
    Layer[SSS]=0;
    que.push_back(SSS);
    while (!que.empty())
    {
        int u=que.front();
        que.pop_front();
        for (int i=1; i<=TTT; i++)
            if (Dis[u][i]>0&&Layer[i]==-1)
            {
                Layer[i]=Layer[u]+1;
                if (i==TTT)
                    return true;
                else
                    que.push_back(i);
            }
    }
    return false;
}
int Dinic()
{
    int Ans=0;
    deque<int> que;
    while (CountLayer())
    {
        que.push_back(SSS);
        memset(Visited,false,sizeof(Visited));
        Visited[SSS]=true;
        while (!que.empty())
        {
            int u=que.back();
            if (u==TTT)
            {
                int MinFlow=999999999,MinFlowStart;
                for (int i=1; i<(int)que.size(); i++)
                {
                    int S=que[i-1],E=que[i];
                    if (Dis[S][E]>0)
                        if (MinFlow>Dis[S][E])
                            MinFlow=Dis[S][E],MinFlowStart=S;
                }
                Ans+=MinFlow;
                for (int i=1; i<(int)que.size(); i++)
                {
                    int S=que[i-1],E=que[i];
                    Dis[S][E]-=MinFlow,Dis[E][S]+=MinFlow;
                }
                while (!que.empty()&&que.back()!=MinFlowStart)
                {
                    Visited[que.back()]=false;
                    que.pop_back();
                }
            }
            else
            {
                int i;
                for (i=1; i<=TTT; i++)
                    if (Dis[u][i]>0&&Layer[i]==Layer[u]+1&&!Visited[i])
                    {
                        Visited[i]=true;
                        que.push_back(i);
                        break;
                    }
                if (i>TTT)
                    que.pop_back();
            }
        }
    }
    return Ans;
}

inline void AddEdge(int a,int b,int c)
{
    Dis[a][b]=c;
}

int main()
{
    int T,N,M;
    scanf("%d",&T);
    while (T--)
    {
        memset(Dis,0,sizeof(Dis));
        scanf("%d%d",&N,&M);
        TTT=204,SSS=0;
        for (int i=1;i<=M;i++)
        {
            cin>>F[i+101];
            AddEdge(i+101,TTT,F[i+101]);
        }
        int a,b,c;
        for (int i=1;i<=N;i++)
        {
            cin>>a>>b;
            AddEdge(SSS,i,a);
            for (int j=0;j<b;j++)
            {
                cin>>c;
                AddEdge(i,c+101,1);
            }
        }
        Dinic();
        bool Ans=true;
        for (int i=1;i<=M;i++)
            if (Dis[i+101][TTT]!=0)
                Ans=false;
        if (Ans)
            cout<<"Yes\n";
        else
            cout<<"No\n";
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值