LOJ #115. Passive sink has upstream and downstream feasible streams,

Source: Internet
Author: User

LOJ #115. Passive sink has upstream and downstream feasible streams,
#115. Description of a passive sink with upstream and downstream feasible streams

This is a template question.

N points, m edge, each side e has a lower flow threshold lower (e) \ text {lower} (e) lower (e) and the upper limit of traffic upper (e) \ text {upper} (e) upper (e), find a feasible solution so that all points meet the traffic balance conditions, all edges meet the traffic limit.

Input Format

The first line has two positive integers n and m.

Next m rows, each row has four integers: s, t, lower \ text {lower} lower, and upper \ text {upper} upper.

Output Format

If there is no solution, output a lineNO.

Otherwise, the first line is output.YESAnd then an integer in each line of m, indicating the traffic of each edge.

Sample input 1
4 61 2 1 22 3 1 23 4 1 24 1 1 21 3 1 24 2 1 2
Sample output 1
NO
Sample input 2
4 61 2 1 32 3 1 33 4 1 34 1 1 31 3 1 34 2 1 3
Sample output 2
YES123211
Data range and prompt

1 ≤ n ≤ 10200, 1 ≤ m ≤ 200 1 \ leq n \ leq 10200, 1 \ leq m \ leq 10200 1 ≤ n ≤, 1 ≤ m ≤

Show category labels

 

The question is not detailed. You can sort it out when you are free.

 

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<queue>using namespace std;const int MAXN=2000001;inline char nc(){    static char buf[MAXN],*p1=buf,*p2=buf;    return p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++;}inline int read(){    char c=nc();int x=0,f=1;    while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();}    while(c>='0'&&c<='9'){x=x*10+c-'0';c=nc();}    return x*f;}int n,m,s,t;struct node{    int u,v,flow,nxt;}edge[MAXN];int head[MAXN],cur[MAXN],A[MAXN];int num=0;void AddEdge(int x,int y,int z){    edge[num].u=x;    edge[num].v=y;    edge[num].flow=z;    edge[num].nxt=head[x];    head[x]=num++;}void add_edge(int x,int y,int z){    AddEdge(x,y,z);    AddEdge(y,x,0);}int deep[MAXN],L[MAXN];bool BFS(){    memset(deep,0,sizeof(deep));    deep[s]=1;    queue<int>q;    q.push(s);    while(q.size()!=0)    {        int p=q.front();        q.pop();        for(int i=head[p];i!=-1;i=edge[i].nxt)            if(!deep[edge[i].v]&&edge[i].flow)            {                deep[edge[i].v]=deep[edge[i].u]+1;q.push(edge[i].v);                if(edge[i].v==t) return 1;            }                    }    return deep[t];    }int DFS(int now,int nowflow){    if(now==t||nowflow<=0)        return nowflow;    int totflow=0;    for(int &i=cur[now];i!=-1;i=edge[i].nxt)    {        if(deep[edge[i].v]==deep[edge[i].u]+1&&edge[i].flow)        {            int canflow=DFS(edge[i].v,min(nowflow,edge[i].flow));            edge[i].flow-=canflow;            edge[i^1].flow+=canflow;            totflow+=canflow;            nowflow-=canflow;            if(nowflow<=0)                break;        }    }    return totflow;}int Dinic(){    int ans=0;    while(BFS())    {        for(int i=0;i<=n;i++)            cur[i]=head[i];        ans+=DFS(s,1e8);    }    return ans;}int main(){    #ifdef WIN32    freopen("a.in","r",stdin);    #else    #endif    n=read();m=read();s=0;t=n+1;    memset(head,-1,sizeof(head));    for(int i=1;i<=m;i++)    {        int x=read(),y=read(),lower=read(),upper=read();L[i-1]=lower;        add_edge(x,y,upper-lower);A[x]-=lower;A[y]+=lower;    }    int sum=0;    for(int i=1;i<=n;i++)    {        if(A[i]>0) sum+=A[i],add_edge(s,i,A[i]);        else add_edge(i,t,-A[i]);    }    if(Dinic()!=sum) printf("NO");    else    {        printf("YES\n");        for(int i=0;i<m;i++)            printf("%d\n",edge[i*2|1].flow+L[i]);    }    return  0;}

 

Related Article

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.