【SGU194】Reactor Cooling 无源汇上下界可行流

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

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 f ij,(put f ij = 0 if there is no pipe from node i to node j),for each i the following condition must hold:

sum(j=1..N,f ij) = sum(j=1..N,f ji)

Each pipe has some finite capacity,therefore for each i and j connected by the pipe must be f ij ≤ c ij where c ij 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 l ij,thus it must be f ij ≥ l ij.

Given c ij and l ij for all pipes,find the amount f ij,satisfying the conditions specified above.

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,l ij and c ij each. There is at most one pipe connecting any two nodes and 0 ≤ l ij ≤ c ij ≤ 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 test(s)

Input

Test #1 

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 

Test #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

Output

Test #1 

NO 

Test #2 

YES 
1 
2 
3 
2 
1 
1

[submit]
[forum]
Author: Andrew Stankevich
Resource: Petrozavodsk Winter Trainings 2003
Date: 2003-02-06


这个论文讲得不错:《一种简易的方法求解流量有上下界的网络中网络流问题》

记录inb[u]表示u的入边的流量下界之和,outb[u]是u的出边的流量下界之和。

建图方法

1.u到v建容量为C[u,v]-B[u,v]的边。
2.对于点u,记 tmp=inb[u]outb[u] 。若tmp>0,则S向u建容量为tmp的边,若tmp<0,则u向e建容量为-tmp的边。

若满流则有解。

简单证明:设< u,v >边上的流量为f,则 f=g+B ,其中B是流量下界,g是超过下界的部分。g的范围是 [B,C] ,但容量设为 CB 跑最大流不一定满足容量守恒,所以我们用流量守恒的式子得到:

f<u,i>=f<i,v>=>@H_570_301@∑g<u,i>+B<u,i>=g@H_133_403@@H_873_404@<i,v>+B<i,v>

@H_403_481@ Mi=B<u,i>B<i,v> ,也就是i点的tmp。
此时:

g<i,v>g<u,i>=Mi

然后讨论:若 Mi<0 g<u,i>=g<i,v>Mi ,则意义是进i号点的流需要多分出去 Mi 个,这就可以跑网络流了,大于0同理。

获得成就:五分钟dinic不打错

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;

const int INF = 1000000010;
const int SZ = 1000010;

int head[SZ],nxt[SZ],tot = 1,s,e;

struct edge{
    int t,d;
}l[SZ];

void build(int f,int t,int d)
{
    l[++ tot].t = t;
    l[tot].d = d;
    nxt[tot] = head[f];
    head[f] = tot;
}

void insert(int f,int d)
{
    build(f,t,d); build(t,f,0);
}


int deep[SZ];
queue<int> q;

bool bfs()
{
    memset(deep,0,sizeof(deep));
    deep[s] = 1;
    while(q.size()) q.pop();
    q.push(s);
    while(q.size())
    {
        int u = q.front(); q.pop();
        for(int i = head[u];i;i = nxt[i])
        {
            int v = l[i].t;
            if(!deep[v] && l[i].d)
            {
                deep[v] = deep[u] + 1;
                q.push(v);
                if(v == e) return true;
            }
        }
    }
    return false;
}

int dfs(int u,int flow)
{
    if(u == e || flow == 0) return flow;
    int rest = flow;
    for(int i = head[u];i;i = nxt[i])
    {
        int v = l[i].t;
        if(l[i].d && deep[v] == deep[u] + 1)
        {
            int f = dfs(v,min(rest,l[i].d));
            if(f > 0)
            {
                l[i].d -= f;
                l[i ^ 1].d += f;
                rest -= f;
                if(rest == 0) break;
            }
            else deep[v] = 0;
        }
    }
    if(flow - rest == 0) deep[u] = 0;
    return flow - rest;
}

int dinic()
{
    int ans = 0;
    while(bfs())
    {
        int tmp = dfs(s,INF);
        if(!tmp) break;
        ans += tmp;
    }
    return ans;
}

int inb[SZ],outb[SZ],B[SZ];

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= m;i ++)
    {
        int s,b,c;
        scanf("%d%d%d%d",&s,&t,&b,&c);
        insert(s,c - b);
        B[i] = b;
        outb[s] += b;
        inb[t] += b;
    }
    s = n + 1,e = n + 2;
    int sum = 0;
    for(int i = 1;i <= n;i ++)
    {
        int tmp = inb[i] - outb[i];
        if(tmp > 0)
            insert(s,i,tmp),sum += tmp;
        else
            insert(i,e,-tmp);
    }

    int ans = dinic();

    if(ans == sum)
    {
        puts("YES");
        for(int i = 1;i <= m;i ++)
            printf("%d\n",B[i] + l[i * 2 + 1].d);
    }
    else
        puts("NO");
    return 0;
}

猜你在找的React相关文章