码迷,mamicode.com
首页 > 其他好文 > 详细

上下界网络流模板

时间:2018-02-23 20:39:02      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:上下   ble   memcpy   oid   mat   mem   amp   git   while   

无源汇上下界可行流

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<bitset>
#include<vector>
#include<cstdlib>
#define QAQ int
#define TAT long long
#define OwO bool
#define ORZ double
#define F(i,j,n) for(QAQ i=j;i<=n;++i)
#define E(i,j,n) for(QAQ i=j;i>=n;--i)
#define MES(i,j) memset(i,j,sizeof(i))
#define MEC(i,j) memcpy(i,j,sizeof(j))

using namespace std;
const QAQ N=305,M=30000;

QAQ n,m,s,t;
struct Link{
    QAQ to,last,val,id;
}a[M];
QAQ head[N],js=1;
QAQ dis[N],use[N],ans[M];
QAQ low[M],flow[N];
OwO vis[N];
queue<QAQ> q;

void add(QAQ x,QAQ y,QAQ z,QAQ p){
    a[++js].to=y;a[js].id=p;a[js].val=z;
    a[js].last=head[x];head[x]=js;
}

OwO bfs(){
    F(i,s,t) dis[i]=vis[i]=0;
    dis[s]=vis[s]=1;
    q.push(s);
    while(!q.empty()){
        QAQ x=q.front();q.pop();
        for(QAQ i=head[x];i;i=a[i].last) if(a[i].val&&!vis[a[i].to]){
            vis[a[i].to]=1;
            dis[a[i].to]=dis[x]+1;
            q.push(a[i].to);
        }
    }
    return vis[t];
}

QAQ dfs(QAQ x,QAQ want){
    if(x==t||!want) return want;
    QAQ f=0,ans=0;
    for(QAQ i=use[x];i;i=a[i].last) if(dis[a[i].to]==dis[x]+1){
        f=dfs(a[i].to,min(a[i].val,want));
        if(!f) continue;
        ans+=f;
        want-=f;
        a[i].val-=f;
        a[i^1].val+=f;
        if(!want) break;
        use[x]=i;
    }
    if(!ans) dis[x]=-1;
    return ans;
}

QAQ dinic(){
    QAQ ans=0;
    while(bfs()){
        MEC(use,head);
        ans+=dfs(s,1e9);
    }
    return ans;
}

QAQ main(){
    scanf("%d%d",&n,&m);
    s=0;t=n+1;
    F(i,1,m){
        QAQ u,v,w;
        scanf("%d%d%d%d",&u,&v,&low[i],&w);
        add(u,v,w-low[i],i);
        add(v,u,0,i);
        flow[v]+=low[i];
        flow[u]-=low[i];
    }
    QAQ sum=0;
    F(i,1,n) if(flow[i]<0){
        add(i,t,-flow[i],0);
        add(t,i,0,0);
    }
    else {
        sum+=flow[i];
        add(s,i,flow[i],0);
        add(i,s,0,0);
    }
    if(dinic()==sum){
        printf("YES\n");
        F(x,1,n) for(QAQ i=head[x];i;i=a[i].last) if(a[i].id==0||i%2==0) continue;
        else {
            ans[a[i].id]=a[i].val+low[a[i].id];
        }
        F(i,1,m) printf("%d\n",ans[i]);
    }
    else printf("NO\n");
    return 0;
}

有源汇有上下界最大流

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<bitset>
#include<vector>
#include<cstdlib>
#define QAQ int
#define TAT long long
#define OwO bool
#define ORZ double
#define F(i,j,n) for(QAQ i=j;i<=n;++i)
#define E(i,j,n) for(QAQ i=j;i>=n;--i)
#define MES(i,j) memset(i,j,sizeof(i))
#define MEC(i,j) memcpy(i,j,sizeof(j))

using namespace std;
const QAQ N=405,M=30000;

QAQ n,m,s,t,ss,tt;
struct Link{
    QAQ to,last,val,id;
}a[M];
QAQ head[N],js=1;
QAQ dis[N],use[N],ans[M];
QAQ low[M],flow[N];
OwO vis[N];
queue<QAQ> q;

void add(QAQ x,QAQ y,QAQ z,QAQ p){
    a[++js].to=y;a[js].id=p;a[js].val=z;
    a[js].last=head[x];head[x]=js;
}

OwO bfs(){
    F(i,0,n+1) dis[i]=vis[i]=0;
    dis[ss]=vis[ss]=1;
    q.push(ss);
    while(!q.empty()){
        QAQ x=q.front();q.pop();
        for(QAQ i=head[x];i;i=a[i].last) if(a[i].val&&!vis[a[i].to]){
            vis[a[i].to]=1;
            dis[a[i].to]=dis[x]+1;
            q.push(a[i].to);
        }
    }
    return vis[tt];
}

QAQ dfs(QAQ x,QAQ want){
    if(x==tt||!want) return want;
    QAQ f=0,ans=0;
    for(QAQ i=use[x];i;i=a[i].last) if(dis[a[i].to]==dis[x]+1){
        f=dfs(a[i].to,min(a[i].val,want));
        if(!f) continue;
        ans+=f;
        want-=f;
        a[i].val-=f;
        a[i^1].val+=f;
        if(!want) break;
        use[x]=i;
    }
    if(!ans) dis[x]=-1;
    return ans;
}

QAQ dinic(){
    QAQ ans=0;
    while(bfs()){
        MEC(use,head);
        ans+=dfs(ss,1e9);
    }
    return ans;
}

QAQ main(){
    scanf("%d%d%d%d",&n,&m,&s,&t);
    ss=0;tt=n+1;
    F(i,1,m){
        QAQ u,v,w;
        scanf("%d%d%d%d",&u,&v,&low[i],&w);
        add(u,v,w-low[i],i);
        add(v,u,0,i);
        flow[v]+=low[i];
        flow[u]-=low[i];
    }
    QAQ sum=0;
    F(i,1,n) if(flow[i]<0){
        add(i,tt,-flow[i],0);
        add(tt,i,0,0);
    }
    else {
        sum+=flow[i];
        add(ss,i,flow[i],0);
        add(i,ss,0,0);
    }
    add(t,s,1e9,0);
    if(dinic()==sum){
        sum=a[head[t]^1].val;
        F(i,2,js) if(!a[i].id) a[i].val=0;
        head[ss]=head[tt]=0;
        ss=s;tt=t;
        printf("%d\n",sum+dinic());
    }
    else printf("please go home to sleep\n");
    return 0;
}

有源汇有上下界最小流

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<bitset>
#include<vector>
#include<cstdlib>
#define QAQ int
#define TAT long long
#define OwO bool
#define ORZ double
#define F(i,j,n) for(QAQ i=j;i<=n;++i)
#define E(i,j,n) for(QAQ i=j;i>=n;--i)
#define MES(i,j) memset(i,j,sizeof(i))
#define MEC(i,j) memcpy(i,j,sizeof(j))

using namespace std;
const QAQ N=70000,M=2000000;

QAQ n,m,s,t,ss,tt;
struct Link{
    QAQ to,last,val,id;
}a[M];
QAQ head[N],js=1;
QAQ dis[N],use[N],ans[M];
QAQ low[M],flow[N];
OwO vis[N];
queue<QAQ> q;

inline void read(int &x){
    int f = 0, ch = 0; x = 0;
    for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
    for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
    if(f) x = -x;
}

void add(QAQ x,QAQ y,QAQ z,QAQ p){
    a[++js].to=y;a[js].id=p;a[js].val=z;
    a[js].last=head[x];head[x]=js;
}

OwO bfs(){
    F(i,0,n+1) dis[i]=vis[i]=0;
    dis[ss]=vis[ss]=1;
    q.push(ss);
    while(!q.empty()){
        QAQ x=q.front();q.pop();
        for(QAQ i=head[x];i;i=a[i].last) if(a[i].val&&!vis[a[i].to]){
            vis[a[i].to]=1;
            dis[a[i].to]=dis[x]+1;
            q.push(a[i].to);
        }
    }
    return vis[tt];
}

QAQ dfs(QAQ x,QAQ want){
    if(x==tt||!want) return want;
    QAQ f=0,ans=0;
    for(QAQ i=use[x];i;i=a[i].last) if(dis[a[i].to]==dis[x]+1){
        f=dfs(a[i].to,min(a[i].val,want));
        if(!f) continue;
        ans+=f;
        want-=f;
        a[i].val-=f;
        a[i^1].val+=f;
        if(!want) break;
        use[x]=i;
    }
    if(!ans) dis[x]=-1;
    return ans;
}

QAQ dinic(){
    QAQ ans=0;
    while(bfs()){
        F(i,0,n+1) use[i]=head[i];
        ans+=dfs(ss,2e9);
    }
    return ans;
}

QAQ main(){
//  freopen("8.in","r",stdin);
    read(n);read(m);read(s);read(t);
//  scanf("%d%d%d%d",&n,&m,&s,&t);
    ss=0;tt=n+1;
    F(i,1,m){
        QAQ u,v,w;
        read(u);read(v);read(low[i]);read(w);
//      scanf("%d%d%d%d",&u,&v,&low[i],&w);
        add(u,v,w-low[i],i);
        add(v,u,0,i);
        flow[v]+=low[i];
        flow[u]-=low[i];
    }
    QAQ sum=0;
    F(i,1,n) if(flow[i]<0){
        add(i,tt,-flow[i],0);
        add(tt,i,0,0);
    }
    else {
        sum+=flow[i];
        add(ss,i,flow[i],0);
        add(i,ss,0,0);
    }
    add(t,s,2e9,0);
    if(dinic()==sum){
        sum=a[head[t]^1].val;
        F(i,2,js) if(!a[i].id) a[i].val=0;
        head[ss]=head[tt]=0;
        ss=t;tt=s;
        printf("%d\n",sum-dinic());
    }
    else printf("please go home to sleep\n");
    return 0;
}

上下界网络流模板

标签:上下   ble   memcpy   oid   mat   mem   amp   git   while   

原文地址:https://www.cnblogs.com/heower/p/8463119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!