zoj2314 Reactor Cooling --- 上下界可行流

题目给出了每条边的上下界,

此类题目的建边方法是:

1、添加源点汇点,

2、对每条边 添加边 c(u,v) = up(u,v) - low(u,v)

3、对每个点 c(s,v) = out(v)

                       c(v,t) = in(v)   (权值为正)

求s到t的最大流,若最大流等于所有边下界的和,则存在可行流,

每条边的流量为 flow(u,v) +low(u,v)


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define inf 0x3f3f3f3f
#define eps 1e-6
#define ll __int64
const int maxn=210;
const int maxm=40010;
using namespace std;

int n,m,s,t,low[maxm],in[maxn],out[maxn];
struct node
{
    int from,to,cap,flow;
};
struct dinic
{
    int n,m,s,t;
    vector e;
    vector g[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];

    void init(int n)
    {
        e.clear();
        for(int i=0;i<=n+1;i++)
            g[i].clear();
    }

    void addedge(int a,int b,int c,int d)//c d为正向弧和反向弧的权值 一般反向弧为0
    {
        e.push_back((node){a,b,c,0});
        e.push_back((node){b,a,d,0});
        m=e.size();
        g[a].push_back(m-2);
        g[b].push_back(m-1);
    }

    bool bfs()
    {
        memset(vis,0,sizeof vis);
        queue q;
        q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!q.empty())
        {
            int x=q.front();q.pop();
            for(int i=0;iee.flow)
                {
                    vis[ee.to]=1;
                    d[ee.to]=d[x]+1;
                    q.push(ee.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int x,int a)
    {
        if(x==t||a==0) return a;
        int flow=0,f;
        for(int& i=cur[x];i0)
            {
                ee.flow+=f;
                e[g[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0) break;
            }
        }
        return flow;
    }

    int maxflow(int s,int t)
    {
        this->s=s;
        this->t=t;
        int flow=0;
        while(bfs())
        {
            memset(cur,0,sizeof cur);
            flow+=dfs(s,inf);
        }
        return flow;
    }

    void output(int cnt)
    {
        for(int i=0;i


你可能感兴趣的:(graphs,network,flows)