BZOJ 3876 有上下界的网络流

思路:

套用有上下界的网络流 就好了   (这算是裸题吧)

 

比如 有条 x->y 的边  流量上限为R 下限为L

那么du[x]-=L,du[y]+=L

流量上限变成R-L

du[x]>0  SS->x 流量为du[x]

否则  x->TT 流量为-du[x]

//By SiriusRen
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int N=666,M=666666,inf=0x3f3f3f3f;
int n,xx,yy,op;
int first[N],next[M],v[M],edge[M],cost[M],tot;
int vis[N],with[N],minn[N],d[N],ans;
void Add(int x,int y,int C,int E){edge[tot]=E,cost[tot]=C,v[tot]=y,next[tot]=first[x],first[x]=tot++;}
void add(int x,int y,int C,int E){Add(x,y,C,E),Add(y,x,-C,0);}
bool tell(){
    mem(vis,0),mem(with,0),mem(minn,0x3f),mem(d,0x3f);
    queue<int>q;q.push(0),d[0]=0;
    while(!q.empty()){
        int t=q.front();q.pop(),vis[t]=0;
        for(int i=first[t];~i;i=next[i])
            if(d[v[i]]>d[t]+cost[i]&&edge[i]){
                minn[v[i]]=min(minn[t],edge[i]),with[v[i]]=i,d[v[i]]=d[t]+cost[i];
                if(!vis[v[i]])vis[v[i]]=1,q.push(v[i]);
        }
    }return d[n+1]!=0x3f3f3f3f;
}
int zeng(){
    for(int i=n+1;i;i=v[with[i]^1])
        edge[with[i]]-=minn[n+1],edge[with[i]^1]+=minn[n+1];
    return minn[n+1]*d[n+1];
}
int main(){
    memset(first,-1,sizeof(first));
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&op);
        add(i,1,0,inf),add(i,n+1,0,op);
        while(op--)scanf("%d%d",&xx,&yy),add(i,xx,yy,inf),add(0,xx,yy,1);
    }
    while(tell())ans+=zeng();
    printf("%d\n",ans);
}

 

posted @ 2017-03-28 22:58  SiriusRen  阅读(294)  评论(0编辑  收藏  举报