ZOJ2314: Reactor Cooling 题解

无源汇的上下界可行流的模板题
上下界的做法可以参考Algorithm Design一书,里面写的很清楚
我们考虑对每个点i维护一个d[i],表示这个点的surve和demand情况,为正表示demand,为负表示serve
我们考虑一条从x到y,下限为low,上限为high的边
如果我们预先从x到y流low的流量,那么这就变成了一条没有下限,上限为high-low的边,全部转换以后就可以跑最大流了
x预先向y流了low的流量,则x多了low的demand,y多了low的surve,即d[x]+=low,d[y]-=low
最后跑最大流的时候,建一个超级源点,向所有d[i]为负的点连流量是-d[i]的边,建一个超级汇点,所有d[i]为正的点向超级汇点连流量是d[i]的边,原图存在可行流当且仅当新图的最大流为D,其中D为 d [ i ] [ d [ i ] 0 ]

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <utility>
#include <cctype>
#include <algorithm>
#include <bitset>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#define LL long long
#define LB long double
#define x first
#define y second
#define Pair pair<int,int>
#define pb push_back
#define pf push_front
#define mp make_pair
#define LOWBIT(x) x & (-x)
using namespace std;

const int MOD=1e9;
const LL LINF=2e16;
const int INF=2e9;
const int magic=348;
const double eps=1e-10;
const double pi=acos(-1);

inline int getint()
{
    char ch;int res;bool f;
    while (!isdigit(ch=getchar()) && ch!='-') {}
    if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
    while (isdigit(ch=getchar())) res=res*10+ch-'0';
    return f?res:-res;
}

int n,e,t;

int d[300048];
int head[300048],to[300048],nxt[300048],f[300048],low[300048],high[300048],type[300048],tot=1;
inline void addedge(int s,int t,int lo,int hi,int ty)
{
    to[++tot]=t;nxt[tot]=head[s];head[s]=tot;low[tot]=lo;high[tot]=hi;f[tot]=hi-lo;type[tot]=ty;
    to[++tot]=s;nxt[tot]=head[t];head[t]=tot;low[tot]=lo;high[tot]=hi;f[tot]=0;type[tot]=2-ty;
}

inline void Clear() {tot=1;memset(head,0,sizeof(head));memset(d,0,sizeof(d));}

int depth[300048];queue<int> q;
inline bool bfs()
{
    int i,x,y;
    for (i=0;i<=t;i++) depth[i]=-1;
    depth[0]=0;q.push(0);
    while (!q.empty())
    {
        x=q.front();q.pop();
        for (i=head[x];i;i=nxt[i])
        {
            y=to[i];
            if (depth[y]==-1 && f[i])
            {
                depth[y]=depth[x]+1;
                q.push(y);
            }
        }
    }
    if (depth[t]==-1) return false; else return true;
}

inline int dfs(int x,int maxf)
{
    if (x==t || !maxf) return maxf;
    int i,y,minf,now,ans=0;
    for (i=head[x];i;i=nxt[i])
    {
        y=to[i];
        if (depth[y]==depth[x]+1 && f[i])
        {
            minf=min(maxf-ans,f[i]);
            now=dfs(y,minf);
            f[i]-=now;f[i^1]+=now;ans+=now;
        }
        if (ans>=maxf) return maxf;
    }
    if (!ans) depth[x]=0;
    return ans;
}

int main ()
{
    int i,ca,lo,hi,x,y;
    ca=getint();
    while (ca--)
    {
        Clear();
        n=getint();e=getint();t=n+1;
        for (i=1;i<=e;i++)
        {
            x=getint();y=getint();lo=getint();hi=getint();
            addedge(x,y,lo,hi,0);
            d[x]+=lo;d[y]-=lo;
        }
        int sum=0;
        for (i=1;i<=n;i++)
        {
            if (d[i]>0) sum+=d[i];
            if (d[i]>0) addedge(i,t,0,d[i],1); else addedge(0,i,0,-d[i],1);
        }
        int ans=0;
        while (bfs()) ans+=dfs(0,2e9);
        if (ans!=sum) {printf("NO\n");continue;}
        printf("YES\n");
        for (i=2;i<=tot;i++)
            if (type[i]==2) printf("%d\n",low[i]+f[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/iceprincess_1968/article/details/80157646
今日推荐