zoukankan      html  css  js  c++  java
  • 【有上下界的网络流】ZOJ2341 Reactor Cooling(有上下界可行流)

     Description

    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 <= cij where 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 lij for 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

    Sample Input

    NO

    YES
    1
    2
    3
    2
    1
    1

    描述

    求有N个点M条边,每条边有上下界的网络图是否有可行解

    题解

    原边的流量改为上界-下界

    然后计算每个点的入边的下界和-出边的下界和x

    如果x>0从s向这个点连边,边权为x

    如果x<0从t向这个点连边,边权为-x

    判断与s相连的边是否都满流

    代码

    //by 减维
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<bitset>
    #include<set>
    #include<cmath>
    #include<vector>
    #include<set>
    #include<map>
    #include<ctime>
    #include<algorithm>
    #define ll long long
    #define li inline
    #define rg register
    #define db double
    #define inf 1<<30
    #define maxn 50010
    #define eps 1e-8
    using namespace std;
    
    inline int read()
    {
        int ret=0;bool fla=0;char ch=getchar();
        while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
        if(ch=='-'){fla=1;ch=getchar();}
        while(ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
        return fla?-ret:ret;
    }
    
    struct edge{
        int to,ne,cap;
    }e[maxn<<4];
    
    int n,m,s,t,ans,sum,ecnt=1,mn[maxn<<4],du[maxn],layer[maxn],head[maxn];
    
    void add(int x,int y,int v)
    {
        ecnt++;e[ecnt]=(edge){y,head[x],v};head[x]=ecnt;
        ecnt++;e[ecnt]=(edge){x,head[y],0};head[y]=ecnt;
    }
    
    bool bfs()
    {
        memset(layer,0,sizeof layer);layer[s]=1;
        queue<int>q;q.push(s);
        while(!q.empty())
        {
            int d=q.front();q.pop();
            for(int i=head[d];i;i=e[i].ne)
            {
                int dd=e[i].to;
                if(e[i].cap>0&&!layer[dd])
                {
                    layer[dd]=layer[d]+1;
                    if(dd==t)return 1;
                    q.push(dd);
                }
            }
        }
        return 0;
    }
    
    int dfs(int x,int cap)
    {
        if(x==t||!cap) return cap;
        int tmp,ret=0;
        for(int i=head[x];i;i=e[i].ne)
        {
            int dd=e[i].to;
            if(e[i].cap&&layer[dd]==layer[x]+1)
            {
                tmp=dfs(dd,min(cap,e[i].cap));
                ret+=tmp;cap-=tmp;
                e[i].cap-=tmp;e[i^1].cap+=tmp;
            }
        }
        return ret;
    }
    
    void dinic()
    {
        while(bfs())ans+=dfs(s,inf);
    }
    
    int main()
    {
        int T;
        T=read();
        while(T--){
            ecnt=1;sum=0;ans=0;
            memset(head,0,sizeof head);
            memset(du,0,sizeof du);
            n=read(),m=read();
            for(int i=1,x,y,v;i<=m;++i)
            {
                x=read(),y=read(),mn[i]=read(),v=read();
                add(x,y,v-mn[i]);
                du[x]-=mn[i];
                du[y]+=mn[i];
            }
            s=0,t=n+1;
            for(int i=1;i<=n;++i)
                if(du[i]>0) add(s,i,du[i]),sum+=du[i];
                else add(i,t,-du[i]);
            dinic();
            if(sum!=ans){
                puts("NO
    ");
                continue;
            }else puts("YES");
            for(int i=1;i<=m;i++)
                printf("%d
    ",e[(i<<1)+1].cap+mn[i]);
            puts("");
        }
        return 0;
    }
  • 相关阅读:
    求公约数和比值
    HTML5 文件上传
    js判断是pc还是移动端
    ssm整合
    如何获取数据表中自增主键的值
    MyBatis全局配置文件标签详解
    MyBatis介绍及使用
    基于Spring MVC的文件上传和下载功能的实现
    Spring IOC容器交给application域对象管理
    SpringMVC的简单介绍及使用
  • 原文地址:https://www.cnblogs.com/rir1715/p/8204542.html
Copyright © 2011-2022 走看看