Maximum flow non-recursive dinic algorithm for hdu3572 water problem

Source: Internet
Author: User
Tags cas

Task Schedule

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4624 Accepted Submission (s): 1516


Problem Descriptionour Geometry Princess XMM has stoped she study in computational geometry to concentrate on her newly op Ened Factory. Her factory had introduced M new machines in order to process the coming N tasks. For the i-th task, the factory have to start processing it on or after day Si, process it for Pi days, and finish the task Before or at day Ei. A machine can have work on one task at a time, and each task can be processed by at a time. However, a task can is interrupted and processed on different machines on different days.
Now she wonders whether he had a feasible schedule to finish all the tasks in time. She turns to the help.

Inputon The first line comes an integer T (t<=20), indicating the number of test cases.

You are given-N (n<=500) and M (m<=200) on the first line of all test case. Then on each of next N lines is three integers Pi, si and Ei (1<=pi, Si, ei<=500), which has the meaning described In the description. It is guaranteed, this in a feasible schedule every task, can be finished would be do before or at its end day.

Outputfor each test case, print ' Case x: ' First, where x is the case number. If there exists a feasible schedule to finish all the tasks, print "Yes", otherwise print "No".

Print a blank line after each test case.

Sample Input24 31 3 5 1 1 42 3 73 5 92 22 1 31 2 2

Sample outputcase 1:yes Case 2:yes

#include <iostream>#include<queue>#include<cstring>#include<cstdio>#include<climits>#defineMax (A, b) a>b?a:b#defineMin (A, b) a<b?a:busing namespacestd;structedge{intS,t,f,next;} edge[1100000];inthead[1010];intcur[1010];intpre[1010];intstack[1100000];intent;intn,m,times,s,t;voidAddintStartintLastintf) {EDGE[ENT].S=start;edge[ent].t=last;edge[ent].f=f;edge[ent].next=head[start];head[start]=ent++; Edge[ent].s=last;edge[ent].t=start;edge[ent].f=0; edge[ent].next=head[last];head[last]=ent++;}BOOLBFsintSintT) {memset (PRE,-1,sizeof(pre)); Pre[s]=0; Queue<int>Q;    Q.push (S);  while(!Q.empty ()) {        inttemp=Q.front ();        Q.pop ();  for(inti=head[temp];i!=-1; i=Edge[i].next) {            intTemp2=edge[i].t; if(pre[temp2]==-1&&edge[i].f) {PRE[TEMP2]=pre[temp]+1;            Q.push (TEMP2); }        }    }    returnpre[t]!=-1;}intDinic (intStartintLast ) {    intflow=0, now;  while(BFS (start,last)) {inttop=0; memcpy (Cur,head,sizeof(head)); intu=start;  while(1)        {            if(U==last)//If the end of the endpoint is found, the intermediate path is processed and the flow is calculated            {                intminn=Int_max;  for(intI=0; i<top;i++)                {                    if(minn>edge[stack[i]].f) {Minn=edge[stack[i]].f; now=i; }} Flow+=Minn;  for(intI=0; i<top;i++) {edge[stack[i]].f-=Minn; Edge[stack[i]^1].f+=Minn; } Top=Now ; U=Edge[stack[top]].s; }             for(inti=cur[u];i!=-1; cur[u]=i=edge[i].next)//find the side that you can go from the U point                if(edge[i].f&&pre[edge[i].t]==pre[u]+1)                     Break; if(cur[u]==-1)//If a viable edge is not found from this point, mark the point and backtrack            {                if(top==0) Break; Pre[u]=-1; U=edge[stack[--Top]].            S }            Else//If you find it, continue running{stack[top++]=Cur[u]; U=edge[cur[u]].t; }        }    }    returnflow;}intMain () {scanf ("%d",&Times );  for(intcas=1; cas<=times;cas++) {ent=0; memset (Head,-1,sizeof(head)); intst,ed,lt; intmaxn=0; intsum=0; S=0; t=1001; scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) {scanf ("%d%d%d",&lt,&st,&ed); MAXN=Max (maxn,ed);  for(intj=st;j<=ed;j++) Add (j,i+ -,1); Add (i+ -, T,LT); Sum+=lt; }         for(intI=1; i<=maxn;i++) Add (s,i,m); if(Dinic (s,t) ==sum) printf ("Case %d:yes\n", CAs); Elseprintf"Case %d:no\n", CAs); printf ("\ n"); }    return 0;}

Maximum flow non-recursive dinic algorithm for hdu3572 water problem

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.