Zoj 3229 with Yuanhui maximum flow template problem with upper and lower bounds

Source: Internet
Author: User

/* Pit Ah, the PE program on the ZOJ was originally WA.

The main idea: a cock silk to take pictures of M goddess. Plan to take photos n days, every day, the maximum number of C goddess photos, photos per day can not be more than D, and to each goddess I photographed there is a limit [Li,ri], for each goddess N days of the total number of photos can not exceed the GI, suppose there is a solution to the maximum number of cocks to take a picture of the goddess, and how ; otherwise output-1. Problem-Solving ideas: Add a source Point St, meeting point SD. St to day I even an upper bound is the Di lower bound of 0 edge, each goddess to the meeting point is a lower bound for the GI upper bound for Oo edge, for each day, the day to the first girl even a [Li. Ri] edges. Build model: Source Point S. End point D. Super Source Point SS, Super endpoint DD. First, infer whether there is a feasible flow that satisfies the upper bound of all edges. The method can be transformed into a feasible flow problem with no Yuanhui and upper and lower bounds.

How to convert it? Add an edge from D to s without a lower bound capacity, then the original image becomes a non-Yuanhui circular flow graph. The next thing, the super source point SS even I (du[i]>0), I-even super sinks (du[i]<0), pairs (ss. DD) for the maximum flow. When Maxflow equals all (du[]>0), there is a viable flow. otherwise not. When there is a viable flow. Delete the super source point SS and the super endpoint DD, and then the (S. d) A maximum flow, at which point the resulting maxflow is the solution to the problem. Why is it? Since the first maxflow () is only for the total amount of traffic that satisfies the nether, the Residual network (S,D) has a lot of free flow (no edges connected to the super source and super sinks) without full flow, all finally getting the maxflow= (the first stream full of the Nether + the free flow of the second can flow).

*/#include <stdio.h> #include <string.h> #include <queue>using namespace std; #define N 1500#define II 400000#define inf 0x3fffffffstruct node {int u,v,w,f,next;} Bian[ii*2];int head[n],yong,dis[n],work[n];void init () {Yong=0;memset (head,-1,sizeof (Head));} void Addbian (int u,int v,int w,int f) {bian[yong].u=u;bian[yong].v=v;bian[yong].w=w;bian[yong].f=f;bian[yong].next= head[u];head[u]=yong++;} void Add (int u,int v,int w,int f) {Addbian (u,v,w,f); Addbian (v,u,0,f);} int min (int a,int b) {return a<b?a:b;} int BFS (int s,int t) {memset (dis,-1,sizeof (dis)); queue<int>q; Q.push (s); dis[s]=0; while (!q.empty ()) {int U=q.front (); Q.pop (); for (int i=head[u];i!=-1;i=bian[i].next) {int v=bian[i].v; if (bian[i].w&&dis[v]==-1) {dis[v]=dis[u]+1; Q.push (v); if (v==t) return 1; }}} return 0;} int dfs (int s,intLimit,int t) {if (s==t) return limit; for (int &i=work[s];i!=-1;i=bian[i].next) {int v=bian[i].v; if (bian[i].w&&dis[v]==dis[s]+1) {int Tt=dfs (v,min (LIMIT,BIAN[I].W), T); if (TT) {bian[i].w-=tt; Bian[i^1].w+=tt; return TT; }}} return 0;} int dinic (int s,int t) {int ans=0; while (BFS (s,t)) {memcpy (work,head,sizeof (head)); while (int Tt=dfs (s,inf,t)) Ans+=tt; } return ans; int main () {int n,m,i,j,k,s,t,s,t,a,b,d,index[ii],id,suma,w[n]; while (scanf ("%d%d", &n,&m)!=eof) {init (); s=0;t=m+n+1; s=t+1; t=s+1; Memset (W,0,sizeof (w)); for (i=1;i<=m;i++) {scanf ("%d", &a); W[t]+=a; W[i+n]-=a; Add (i+n,t,inf-a,0); } id=0; for (i=1;i<=n;i++) {scanf ("%d%d", &a,&b); Add (s,i,b,b); while (a--) {scanf ("%d%d%d", &AMP;J,&AMP;K,&AMP;D); j + +; Index[id++]=yong; W[i]-=k; W[j+n]+=k; Add (i,j+n,d-k,d); }} Add (T,s,inf,inf); suma=0; for (i=s;i<=t;i++) {if (w[i]>0) {suma+=w[i]; Add (s,i,w[i],0); } else Add (i,t,-w[i],0); } int F=dinic (S,T); if (F==suma) {head[s]=head[t]=-1; printf ("%d\n", Dinic (s,t)); for (i=0;i<id;i++) printf ("%d\n", BIAN[INDEX[I]].F-BIAN[INDEX[I]].W); } else printf (" -1\n"); printf ("\ n"); }return 0;}


Zoj 3229 with Yuanhui maximum flow template problem with upper and lower bounds

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.