Passive non-sink upper and lower bound network flow (cyclic flow) ZOJ 2314 Reactor Cooling

Source: Internet
Author: User
Tags bool time limit

Today's exam has the upper bound ... I wanted to write about it last night. The result did not write on GG ...

Start learning the upper Nether network stream

Process reference Hengshui big guy Liu_renda


Start the initial study spicy ~


First look at the question


Reactor Cooling Time Limit:5 Seconds Memory limit:32768 KB Special Judge

The terrorist group leaded by a well known international terrorist Ben Bladen are buliding a nuclear reactor to produce PLU Tonium for the nuclear bomb they is planning to create. Being The wicked computer genius of this group, you is 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 is connected at special points, called nodes, each pipe have the starting node and the end point. The liquid must flow by the pipe is from its-start point-to-its-end point and not in the opposite direction.

Let the nodes is numbered from 1 to N. The cooling system must be designed so, 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's, 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 have some finite capacity, therefore for each I and J connected by the pipe must being Fij <= CIJ where CIJ is th e 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 is 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 was an integer N and then a blank line followed by N input blocks. Each input block was 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 <=)-The number of nodes and and m-the number of pipes. The following M lines contain four integers number each-i, J, Lij and CIJ each. There is at the most one pipe connecting any, 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 are no pipe from j-th node to i-th.

Output

On the first line of the output file print YES If there is the the same as the the-carry out reactor cooling and NO if there are none. In the first case M integers must follow, k-th number being the amount of the liquid flowing by the k-th pipe. Pipes is numbered as they is 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

He does not understand English.

Is the bare passive non-sink network stream (circular flow) asking if there is a solution

Output a viable stream with a solution.


First we have to assume that we have mastered all the pre-skills (Dinic is enough)


Then it's going to be swollen.


Just a little bit yy

: There is no solution first, the upper bound of the lower bound to run the maximum flow is not nearly AH: (

Of course it is.

I'm done with the cut-off perimeter. Network of residual quantities

So what we need to consider is how to solve the nether.

Since the nether is not the same, there is no guarantee that the nether is balanced after direct clipping

Then we need to force it to balance.

What the hell?

Since the traffic here is unbalanced,

Then we can use the Yuanhui in the network flow to make it balance

We record incoming outflows in the Nether network

Take a look at this point get the traffic is more is less

If you need more, you can give it from S.

Last run the biggest flow of s->t, see if you can fill the flow is good


Because BJ giant lazy, brain small do not want to write more, may speak of bad

If I don't understand, I'm sorry I wasted your time.

Recommended Dalao Blog for you


#include <cmath> #include <ctime> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <

string> #include <bitset> #include <queue> #include <set> #include <map> using namespace std;

typedef long Long LL;
	inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
	Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
	while (ch>= ' 0 ' &&ch<= ' 9 ') {x= (x<<1) + (x<<3) +ch-' 0 '; Ch=getchar ();}
return x*f;

} void print (int x) {if (x<0) X=-x,putchar ('-'); if (x>=10) print (X/10);p Utchar (x%10+ ' 0 ');}

const int n=210,m=100100,inf=-0x3f3f3f3f;

int m,n,s=n-2,t=n-1;
int ecnt=1,last[n]; struct Edge{int to,nt,val;}
E[M]; inline void readd (int u,int v,int val) {e[++ecnt]= (EDGE) {v,last[u],val};last[u]=ecnt;} inline void Add (int u,int v,int val

{readd (u,v,val); Readd (v,u,0);}

int in[n],low[m]; void Initial () {Ecnt=1;memset (lasT,0,sizeof (last)), memset (In,0,sizeof (in));

int d[n],q[n];
	BOOL BFs () {memset (d,0,sizeof (d)); Memset (q,0,sizeof (q));
	register int i,u,head=0,tail=1;
	Q[0]=s;d[s]=1;
		while (Head<tail) {u=q[head++];
			for (I=last[u];i;i=e[i].nt) if (e[i].val&&!d[e[i].to]) {d[e[i].to]=d[u]+1;
		q[tail++]=e[i].to;
}} return d[t]; } int dfs (int u,int lim) {if (t==u| |!
	Lim) return Lim;
	int res=0,tmp;
		for (int i=last[u];i;i=e[i].nt) if (e[i].val&&d[e[i].to]==d[u]+1) {Tmp=dfs (E[i].to,min (lim,e[i].val));
		e[i].val-=tmp;e[i^1].val+=tmp;res+=tmp;lim-=tmp;
	if (!tmp) d[e[i].to]=-1;if (!lim) break;
} return res;

} void Dinic () {while (BFS ())) Dfs (S,inf);}

void Build () {for (int i=1;i<=n;++i) In[i]>0?add (S,i,in[i]): Add (I,t,-in[i]);}

inline bool Check () {for (int i=last[s];i;i=e[i].nt) if (e[i].val) return 0;return 1;}
	int main () {int t=read ();
	register int i,u,v,val;
		while (t--) {n=read (); M=read ();
		Initial (); for (i=1;i<=m;++i) {u=read (); V=read (); low[i]=read(); Val=read ();
			In[u]-=low[i];in[v]+=low[i];
		Add (U,v,val-low[i]);
		} build ();d inic ();
		if (!check ()) {puts ("NO");p UTS (""); continue;}
		Puts ("YES");
		for (i=1;i<=m;++i) print (e[(i<<1) ^1].val+low[i]), puts ("");
	Puts ("");
} return 0;
}/* 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 NO YES 1 2 3 2 1 1 * *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.