Sgu 194 upstream and downstream network streams

Source: Internet
Author: User
Another template was created. This template should be OK, enough to cope with various network streams. Question: Give N points and m edges, there are two limits on the traffic of each edge. The limit cannot be greater than R or less than L. check whether there is a feasible solution.
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn = 1000000;const int oo = 0x3777777;struct arc{    int to,cap,flow,next;    arc(int x = 0,int y=0,int z = 0,int u =0){        to = x;cap = y;        flow = z;next = u;    }};arc e[maxn];int head[maxn],d[maxn],tot,id[maxn],work[maxn];int S,T,q[maxn],hd,tail,n,m;void add(int u,int v,int cap,int flow,int ID){    id[tot] = ID;    e[tot] = arc(v,cap,flow,head[u]);    head[u] = tot++;    id[tot] = 0;    e[tot] = arc(u,0,-flow,head[v]);    head[v] = tot++;}bool bfs(){    for(int i=1;i<=T;i++)    d[i] = 0;    d[S] = 1;    hd = tail = 0;    q[tail++] = S;    while(hd != tail){        int u = q[hd++];        if(hd == maxn)hd  =0;        for(int i=head[u];i!=-1;i=e[i].next){             if(e[i].cap > e[i].flow && !d[e[i].to]){                d[e[i].to] = d[u] + 1;                q[tail++] = e[i].to;                if(tail == maxn)tail = 0;                if(T == e[i].to)return true;             }        }    }    return false;}int dfs(int u,int low){    int tmp = 0,f;    if(u==T||!low)return low;    for(int &i=work[u];i!=-1;i=e[i].next){            if(d[e[i].to] == d[u] + 1 && (f=dfs(e[i].to,min(low,e[i].cap - e[i].flow)))){               // tmp += f;                e[i].flow += f;                e[i^1].flow -= f;              //  low -= f;              //  if(!low)break;              return f;            }    }    return 0;}void init(){    memset(head,-1,sizeof(head));    tot = 0;}int du[maxn],dn[maxn],ans[maxn];int main(){    int u,v,down,up;    while(~scanf("%d%d",&n,&m)){        init();        for(int i=1;i<=m;i++){            scanf("%d%d%d%d",&u,&v,&down,&up);            add(u,v,up-down,0,i);            du[u] -= down;            du[v] += down;            dn[i] = down;        }        S = 0;T=n+1;        int Edge = tot;        for(int i=1;i<=n;i++){            if(du[i] > 0) add(S,i,du[i],0,0);            if(du[i] < 0) add(i,T,-du[i],0,0);        }        while(bfs())        {            for(int i=0; i<n+2; i++)            work[i]=head[i];            while(dfs(S,oo));        }        int flag = 0;        for(int i=head[S];i!=-1;i=e[i].next){            if(e[i].flow != e[i].cap){                flag = 1;break;            }        }        if(flag)puts("NO");        else {            puts("YES");            for(int i = 0;i<Edge;i++) ans[id[i]] = e[i].flow;            for(int i=1;i<=m;i++)                printf("%d\n",ans[i]+dn[i]);        }    }}


Sgu 194 upstream and downstream network streams

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.