太空飞行计划问题( 24题 )

 最大闭合权子图复习

最后的 s 所在的连通分量为 闭合子图

#include <bits/stdc++.h>
using namespace std;
typedef int LL;
typedef int lint;
const LL maxn = 1000;
const LL maxm = 1000000;
const LL inf = 0x3f3f3f3f;
struct dinic{
    LL he[maxn],ne[maxm],ver[maxm],edge[maxm],d[maxn],vis[maxn];
    vector<lint> ans;
    LL s,t,tot;
    void init( LL n ){
        for( LL i = 0;i <= n;i++ ){
            he[i] = 0;
        }
        tot = 1;
    }
    void add( LL x,LL y,LL z ){
        ver[++tot] = y; ne[ tot ] = he[x]; he[x] = tot; edge[ tot ] = z;
        ver[++tot] = x; ne[tot] = he[y]; he[y] = tot;edge[tot] = 0;
    }
    bool bfs(){
        memset( d,0,sizeof( d ) );
        while( que.size() ) que.pop();
        que.push( s );
        d[s] = 1;
        while( que.size() ){
            LL x = que.front();
            que.pop();
            for( LL cure = he[x];cure;cure = ne[cure] ){
                LL y = ver[cure];
                if( edge[cure] && !d[y] ){
                    que.push( y );
                    d[ y ] = d[x] + 1;
                    if( y == t ) return 1;
                }
            }
        }
        return 0;
    }
    LL dfs( LL x,LL flow ){
        if( x== t ) return flow;
        LL rest = flow,k;
        for( LL cure = he[x];cure && rest;cure = ne[cure] ){
            LL y = ver[cure];
            if( edge[cure] && d[ y ] == d[x] + 1 ){
                k = dfs( y,min( rest,edge[cure] ) );
                if( !k ) d[ y ] = 0;
                edge[cure] -= k;
                edge[ cure^(LL)1 ] += k;
                rest -= k;
            }
        }
        return flow - rest;
    }
    LL max_flow( LL x,LL y ){
        s = x; t = y;
        LL maxflow = 0;
        LL flow = 0;
        while( bfs() )
            while( flow = dfs( s,inf ) ) maxflow += flow;
        return maxflow;
    }
    queue<lint> que;
    void bfs( lint n ){
        vis[s] = 1;
        que.push(s);
        while( que.size() ){
            lint x = que.front();
            que.pop();
            for( lint cure = he[x];cure;cure = ne[cure] ){
                lint y = ver[cure];
                if( edge[cure] <= 0 || vis[y] ) continue;
                vis[y] = 1;
                que.push(y);
            }
        }
        for( lint  i = 1;i <= n;i++ ){
            if( vis[i] )ans.push_back(i);
        }
    }
}  g;
lint val[maxn];
vector<lint> ve[maxn],yiqi;
int main()
{
    lint n,m,num,sum = 0,ulen;
    scanf("%d%d",&n,&m);
    char c[10001];g.tot = 1;
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&val[i]);
        sum+=val[i];
        memset(c,0,sizeof(c));
        cin.getline(c,10000);
        ulen=0;
        while (sscanf(c+ulen,"%d",&num)==1)
        {
            //req[i].pb(num);
            ve[i].push_back( num );
            if (num==0) ulen++;
            else while (num)
                {
                    num/=10;
                    ulen++;
                }
            ulen++;
        }
    }
    lint SS = 0,TT  = n+m+1;
    for (lint i=1;i<=m;i++) {
        scanf("%d", &val[n+i]);
    }
    for( lint i = 1;i <= n+m;i++ ){
        if( i <= n ){
            g.add( SS,i,val[i] );
        }else{
            g.add( i,TT,val[i] );
        }
    }
    for( lint i = 1;i <= n;i++ ){
        for( lint j = 0;j < ve[i].size();j++ ){
            lint y = ve[i][j]+n;
            g.add( i,y,inf );
        }
    }
    lint ans = g.max_flow(SS,TT);
    g.bfs(n);
    lint res =  sum - ans ;
    for( lint i = 0;i < g.ans.size();i++ ){
        lint x= g.ans[i];
        if( i == 0 ) printf("%d",x);
        else printf(" %d",x);
        for( lint j = 0;j < ve[x].size();j++ ){
            lint y = ve[x][j];
            yiqi.push_back( y );
        }
    }
    printf("\n");
    sort( yiqi.begin(),yiqi.end() );
    yiqi.erase( unique( yiqi.begin(),yiqi.end() ),yiqi.end() );
    for( lint i = 0;i < yiqi.size();i++ ){
        if( i == 0 ){
            printf("%d",yiqi[i]);
        }else printf(" %d",yiqi[i]);
    }
    printf("\n");
    printf("%d\n",res);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值