zoj 2314 Reactor Cooling(无源汇上下界的可行流)

前端之家收集整理的这篇文章主要介绍了zoj 2314 Reactor Cooling(无源汇上下界的可行流)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Reactor Cooling

Time Limit:5 Seconds Memory Limit:32768 KB Special Judge

The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear reactor to produce plutonium for the nuclear bomb they are planning to create. Being the wicked computer genius of this group,you are responsible for developing the cooling system for the reactor.

The cooling system of the reactor consists of the number of pipes that special cooling liquid flows by. Pipes are connected at special points,called nodes,each pipe has the starting node and the end point. The liquid must flow by the pipe from its start point to its end point and not in the opposite direction.

Let the nodes be numbered from 1 to N. The cooling system must be designed so that the liquid is circulating by the pipes and the amount of the liquid coming to each node (in the unit of time) is equal to the amount of liquid leaving the node. That is,if we designate the amount of liquid going by the pipe from i-th node to j-th as fij,(put fij= 0 if there is no pipe from node i to node j),for each i the following condition must hold:

f i,1+f i,2+...+f i,N= f 1,i+f 2,i+...+f N,i

Each pipe has some finite capacity,therefore for each i and j connected by the pipe must be fij<= cijwhere cij is the capacity of the pipe. To provide sufficient cooling,the amount of the liquid flowing by the pipe going from i-th to j-th nodes must be at least lij,thus it must be fij>= lij.

Given cij and lijfor all pipes,find the amount fij,satisfying the conditions specified above.


This problem contains multiple test cases!

The first line of a multiple input is an integer N,then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Input

The first line of the input file contains the number N (1 <= N <= 200) - the number of nodes and and M - the number of pipes. The following M lines contain four integer number each - i,j,lij and cij each. There is at most one pipe connecting any two nodes and 0 <= lij<= cij<= 10^5 for all pipes. No pipe connects a node to itself. If there is a pipe from i-th node to j-th,there is no pipe from j-th node to i-th.


Output

On the first line of the output file print YES if there is the way to carry out reactor cooling and NO if there is none. In the first case M integers must follow,k-th number being the amount of liquid flowing by the k-th pipe. Pipes are numbered as they are given in the input file.


Sample Input

2

4 6
1 2 1 2
2 3 1 2
3 4 1 2
4 1 1 2
1 3 1 2
4 2 1 2

4 6
1 2 1 3
2 3 1 3
3 4 1 3
4 1 1 3
1 3 1 3
4 2 1 3


NO

YES
1
2
3
2
1
1


管道有最低流量限制 最高流量限制

对于给定的数据 每个管道是否有满足条件的流量


典型的可行流问题

设有超级源点s 超级汇点t

每个管道建边 权值为c-l

记录每个点向外 向内流出的流量 流出的减 流进的加

如果为正 s->i 权值 out[i]

否则 i->t 权值-out[i]

跑最大流 看结果是否是满流

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <queue>
#include <vector>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl;
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

struct Edge
{
    int from,to,cap,flow;
    bool operator <(const Edge e) const
    {
        if(e.from!=from)  return from<e.from;
        else return to<e.to;
    }
    Edge() {}
    Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow) {}
};

struct Dinic
{
    vector<Edge> edges;
    vector<int> G[MAXN];
    bool vis[MAXN];//BFS使用
    int d[MAXN];   //从起点到i的距离
    int cur[MAXN]; //当前弧下标
    int n,m,s,t,maxflow;   //节点数 边数(包括反向弧) 源点编号和弧点编号

    void init(int n)
    {
        this->n=n;
        for(int i=0;i<=n;i++)
            G[i].clear();
        edges.clear();
    }

    void addedge(int from,int cap)
    {
        edges.push_back(Edge(from,0));
        edges.push_back(Edge(to,from,0));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool bfs()
    {
        MEM(vis,0);
        MEM(d,-1);
        queue<int> q;
        q.push(s);
        d[s]=maxflow=0;
        vis[s]=1;
        while(!q.empty())
        {
            int u=q.front(); q.pop();
            int sz=G[u].size();
            for(int i=0;i<sz;i++)
            {
                Edge e=edges[G[u][i]];
                if(!vis[e.to]&&e.cap>e.flow)
                {
                    d[e.to]=d[u]+1;
                    vis[e.to]=1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int u,int a)
    {
        if(u==t||a==0)  return a;
        int sz=G[u].size();
        int flow=0,f;
        for(int &i=cur[u];i<sz;i++)
        {
            Edge &e=edges[G[u][i]];
            if(d[u]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[u][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())
        {
            MEM(cur,0);
            flow+=dfs(s,INF);
        }
        return flow;
    }
}Dic;

int out[220];
int low[40010];

int main()
{
//    fread;
    int tc;
    scanf("%d",&tc);
    while(tc--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        MEM(out,0);
        int s=0,t=n+1;
        Dic.init(t);
        for(int i=0;i<m;i++)
        {
            int u,v,c;
            scanf("%d%d%d%d",&u,&v,&low[i],&c);
            out[u]-=low[i];
            out[v]+=low[i];
            Dic.addedge(u,c-low[i]);
        }
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            if(out[i]>0)
            {
                Dic.addedge(s,i,out[i]);
                sum+=out[i];
            }
            else
                Dic.addedge(i,-out[i]);
        }
        if(Dic.Maxflow(s,t)==sum)
        {
            puts("YES");
            for(int i=0;i<m;i++)
            {
                printf("%d\n",Dic.edges[i*2].flow+low[i]);
            }
        }
        else
            puts("NO");
        puts("");
    }
    return 0;
}

猜你在找的React相关文章