# Sug176 flow construction (the smallest stream with the upper and lower bounds)

Source: Internet
Author: User
Tags integer numbers

176. Flow constructiontime limit per test: 1 sec.
Memory limit per test: 4096 kbinput: Standard
Output: Standard

You have given the net consisting of nodes and pipes; pipes connect the nodes. Some substance can flow by pipes, and flow speed in any pipe doesn' t exceed capacity of this pipe.
The substance cannot be accumulated in the nodes. But it is being produced in the first node with the non-negative speed and being consumed with the same speed in the last node.
You have some subset taken from the set of pipes of this net. you need to start the motion of substance in the net, and your motion must fully fill the pipes of the given subset. speed of the producing substance in the first node must be minimal.
Calculate this speed and show the scene of substance motion.
Remember that substance can't be accumulated in the nodes of the net.InputTwo positive integer numbers N (1 <= n <= 100) and m have been written in the first line of the Input-numbers of nodes and pipes.
There are m lines follows: each line contains four integer numbers UI, Vi, Zi, CI; the numbers are separated by a space. UI is the beginning of I-th pipe, VI is its end, Zi is a capacity of I-th pipe (1 <= Zi <= 10 ^ 5) and Ci is 1 if I-th pipe must be fully filled,
And 0 otherwise.
Any pair of nodes can be connected only by one pipe. if there is a pipe from node A to Node B, then there is no pipe from B to. not a single node is connected with itself.
There is no pipe which connects nodes number 1 and N. substance can flow only from the beginning of a pipe to its end.OutputWrite one integer number in the first line of the output-it ought to be the minimal speed of the producing substance in the first node.
Write M integers in the second line-I-th number ought to be the flow speed in the I-th pipe (numbering of pipes is equal to the input ).
If it is impossible to fill the given subset, write "impossible ".Sample test (s)Input

Input 1: 4 4 1 2 2 0 2 1 1 1 3 2 1 3 3 4 3 0 input 2: 4 4 1 2 1 0 2 2 1 3 3 3 3 4 2 0 output
Output 1:

3
1 1 2 2
Output 2:
Impossible

Analysis: this topic is the smallest stream algorithm with the upper and lower bounds. If you have learned it, I still don't quite understand it. Please kindly advise...

Look at the figure:

Classic Question: The smallest stream with the upper and lower bounds.

1. In [v] indicates the sum of the lower bound of the edge ending with V, and out [u] indicates the sum of the lower bound of the edge starting with u.

2. Virtual Output SS, TT, connection (SS, in [v]) and (out [u], TT)
3. Perform maxflow (SS, TT) once)
4. Add a t-> s inf edge
5. Run maxflow (SS, TT) again)

6. If the sum of the two maxflows is <in, no feasible stream exists.

7. The final answer is f [T-> S], where both INF-residue [T-> S]

====================

Why should we press the red part? According to Hu botao's paper, it seems that we first add the INF edge of T-> S, then maximize the stream, and then remove irrelevant edges, and then the largest stream in the reverse direction, but it will not be able to run to the 6th groups ....

Even though it is AC, it is still puzzled !!!

`#include<cstdio>using namespace std;const int mm=11111;const int mn=111;const int oo=1000000000;int node,src,dest,edge,n,m;int ver[mm],flow[mm],ans[mm],id[mm],next[mm];int head[mn],work[mn],dis[mn],q[mn],in[mn];inline int min(int a,int b){    return a<b?a:b;}inline void prepare(int _node,int _src,int _dest){    node=_node,src=_src,dest=_dest;    for(int i=0;i<node;++i)head[i]=-1;    edge=0;}inline void addedge(int u,int v,int c,int ID){    ver[edge]=v,flow[edge]=c,id[edge]=ID,next[edge]=head[u],head[u]=edge++;    ver[edge]=u,flow[edge]=0,id[edge]=0,next[edge]=head[v],head[v]=edge++;}bool Dinic_bfs(){    int i,u,v,l,r=0;    for(i=0;i<node;++i)dis[i]=-1;    dis[q[r++]=src]=0;    for(l=0;l<r;++l)        for(i=head[u=q[l]];i>=0;i=next[i])            if(flow[i]>0&&dis[v=ver[i]]<0)            {                dis[q[r++]=v]=dis[u]+1;                if(v==dest)return 1;            }    return 0;}int Dinic_dfs(int u,int exp){    if(u==dest)return exp;    for(int &i=work[u],v,tmp;i>=0;i=next[i])        if(flow[i]>0&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)        {            flow[i]-=tmp;            flow[i^1]+=tmp;            return tmp;        }    return 0;}void Dinic_flow(){    while(Dinic_bfs())    {        for(int i=0;i<node;++i)work[i]=head[i];        while(Dinic_dfs(src,oo));    }}void limit_min_flow(){    int i,src0,dest0,edge0,ret=0;    src0=src,dest0=dest,edge0=edge;    src=node,dest=node+1;    head[src]=head[dest]=-1;    for(i=1;i<node;++i)        if(in[i]>0)addedge(src,i,in[i],0);        else addedge(i,dest,-in[i],0);    node+=2;    Dinic_flow();    addedge(dest0,src0,oo,0);    Dinic_flow();    for(i=head[src];i>=0;i=next[i])        if(flow[i]>0)        {            printf("Impossible\n");            return;        }    for(i=head[dest0];i>=0;i=next[i])        if(ver[i]==src0)break;    if(i<0)    {        printf("Impossible\n");        return;    }    ret=flow[i^1];    printf("%d\n",ret);    for(i=0;i<edge0;++i)ans[id[i]]=flow[i^1];    for(i=1;i<=m;++i)printf("%d ",ans[i]);    printf("\n");}int main(){    //freopen("a.in","r",stdin);    //freopen("a.out","w",stdout);    int i,u,v,k,c;    while(scanf("%d%d",&n,&m)!=-1)    {        for(i=0;i<=n;++i)in[i]=0;        prepare(n+1,1,n);        for(i=1;i<=m;++i)        {            scanf("%d%d%d%d",&u,&v,&c,&k);            if(k)in[u]-=c,in[v]+=c,ans[i]=c;            else addedge(u,v,c,i);        }        limit_min_flow();    }    return 0;}`
Related Keywords:

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.