【bzoj1391】order

这个题还是很机智的。。
最大权闭合子图是怎样通过最小割来限制的 再此基础上稍微变一变。。
但是直接交上去会T 需要用当前弧优化
当前弧优化就是在dfs的时候将流量已经不能再增加的边标记出来 下次dfs的时候直接跳过这些边

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cmath>
using namespace std;
const int N=2511;
const int M=N*2000;
const int inf=0x3f3f3f3f;
int head[N],next[M],key[M],w[M],tot;
int dist[N],inq[N];queue<int>q;
int S,T;
int cur[N];//当前弧 


void bili()
{   
    tot=-1;
    memset(head,-1,sizeof(head));
} 
void add(int x,int y,int z)
{
    tot++;next[tot]=head[x];head[x]=tot;key[tot]=y;w[tot]=z;
    tot++;next[tot]=head[y];head[y]=tot;key[tot]=x;w[tot]=0;
}
int bfs()
{
    memset(dist,-1,sizeof(dist));
    dist[S]=0;
    q.push(S);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        inq[x]=0;
        for(int i=head[x];i!=-1;i=next[i])
        {
           int y=key[i];
           if(w[i]&&dist[y]==-1)
             {
               dist[y]=dist[x]+1;
               if(inq[y]==0)q.push(y),inq[y]=1;
             }
        }
    }
    if(dist[T]==-1)return 0;
    else return 1;
}
int zeng(int x,int b)
{
    if(x==T)return b;
    int now=0;
    for(int i=cur[x];i!=-1&&now<b;i=next[i])
    { 
        int y=key[i];
        if(dist[y]==dist[x]+1&&w[i])
        {
            int temp=zeng(y,min(b-now,w[i]));
            now+=temp;
            w[i]-=temp;
            w[i^1]+=temp;
            if(w[i]>0)cur[x]=i;
        }

    }
    if(now==0)dist[x]=-1;
    return now;
}
int main()
{
//  cout<<64*1024*1024/4;
     bili();
     int n,m;
     cin>>n>>m;
     S=0,T=n+m+1;
     int sum=0;
     for(int i=1;i<=n;i++)
     {
        int wi,si;
        scanf("%d%d",&wi,&si);
        add(S,i,wi);sum+=wi;
        for(int j=1;j<=si;j++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(i,n+x,y);
        }
     }
     for(int i=1;i<=m;i++)
     {
        int x;
        scanf("%d",&x);
        add(i+n,T,x);
     }
     int ans=0;
     while(bfs())
     {
       for(int i=S;i<=T;i++)cur[i]=head[i]; 
       ans+=zeng(S,inf);    
     }

     printf("%d",sum-ans);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值