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 <= 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根管道,每根管道用来流躺液体,单向的,每时每刻每根管道流进来的物质要等于流出去的物质,要使得m条管道组成一个循环体,里面流躺物质。并且满足每根管道一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri,同时最小不能低于Li。

思路:

设上界用ci表示,下界用bi表示。

下界是必须流满的,那么对于每一条边,去掉下界后,其自由流为ci– bi,将每条边的容量设为ci– bi。

但这时仍需要满足:每一个点流进来的流=流出去的流。所以

对于每一个点i,令sum(b(u,i))为所有流进i点的下界流总和,sum(b(i,v))为所有从i点流出去的下界流总和

令du[i]=sum(b(u,i)) -sum(b(i,v))

如果du[i]大于0,代表此点必须还要流出去du[i]的自由流,那么我们从源点连一条du[i]的边到该点。

如果du[i]小于0,代表此点必须还要流进来du[i]的自由流,那么我们从该点连一条-du[i]的边到汇点。

然后求S->T的最大流,看是否满流(S的相邻边都流满)。

满流则有解,否则无解。


AC代码

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <vector>
#include <algorithm>
#define ll long long
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)
using namespace std;

const int INF = 1e9;
const int maxn = 1005;

struct Edge{
    int u,v,cap,flow,next;
}et[maxn * maxn];
int low[maxn],cnt[maxn],dis[maxn],pre[maxn],cur[maxn],eh[maxn],du[maxn],l[maxn * maxn];
int n,m,s,t,num;
void init(){
    memset(eh,-1,sizeof(eh));
    memset(du,sizeof(du));
    num = 0;
}
void add(int u,int v,int cap,int flow){
    Edge e = {u,eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u,int cap){
    add(u,0);
    add(v,u,0);
}
int isap(int s,int t,int nv){
    int u,now,flow = 0;
    memset(low,sizeof(low));
    memset(cnt,sizeof(cnt));
    memset(dis,sizeof(dis));
    for(u = 0; u <= nv; u++) cur[u] = eh[u];
    low[s] = INF,cnt[0] = nv,u = s;
    while(dis[s] < nv)
    {
        for(now = cur[u]; now != -1; now = et[now].next)
        if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;
        if(now != -1)
        {
            cur[u] = pre[v] = now;
            low[v] = min(low[u],et[now].cap - et[now].flow);
            u = v;
            if(u == t)
            {
                for(; u != s; u = et[pre[u]].u)
                {
                    et[pre[u]].flow += low[t];
                    et[pre[u]^1].flow -= low[t];
                }
                flow += low[t];
                low[s] = INF;
            }
        }
        else
        {
            if(--cnt[dis[u]] == 0) break;
            dis[u] = nv,cur[u] = eh[u];
            for(now = eh[u]; now != -1; now = et[now].next)
            if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)
            dis[u] = dis[et[now].v] + 1;
            cnt[dis[u]]++;
            if(u != s) u = et[pre[u]].u;
        }
    }
    return flow;
}
void solve(){
    isap(s,t + 1);
    for(int i = eh[s]; i != -1; i = et[i].next)
    if(et[i].cap - et[i].flow)
    {
        printf("NO\n");
        return;
    }
    printf("YES\n");
    for(int i = 0; i < m; i++)
    printf("%d\n",et[i * 2].flow + l[i]);
}
int main()
{
    int a,b,c,tt;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&n,&m);
        init();
        s = 0;
        t = n + 1;
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d%d",&a,&b,&l[i],&c);
            addedge(a,c - l[i]);
            du[a] -= l[i];
            du[b] += l[i];
        }
        for(int i = 1; i <= n; i++)
        {
            if(du[i] > 0) addedge(s,i,du[i]);
            else if(du[i] < 0) addedge(i,-du[i]);
        }
        solve();
    }
    return 0;
}
原文链接:https://www.f2er.com/react/308154.html

猜你在找的React相关文章