Reprint Please specify source: http://www.cnblogs.com/fraud/--by fraud
Test instructions: A store is open 24 hours a day, but each time period requires a different teller, the existing n individuals apply for the job, which can work for eight hours from a fixed time t, ask the minimum number of cashier to meet the needs of the situation
A very classical differential constraint problem with a slightly more difficult composition
To avoid negative numbers, the time count 1~24.
Make:
R[i] Represents the number of people I need in time (1<=i<=24)
Num[i] Indicates the number of applicants for I time (1<=i<=24)
X[i] Represents the number of people I have hired (0<=I<=24), which makes x[0]=0
Then set S[i]=x[0]+x[1]+......+x[i] (0<=i<=24),
By test instructions, the following equations can be obtained:
(1) s[i]-s[i-8]>=r[i] (8<=i<=24)
(2) s[i]-s[16+i]>=r[i]-s[24] (1<=i<=7)
(3) s[i]-s[i-1]>=0 (1<=i<=24)
(4) S[i-1]-s[i]>=-t[i] (1<=i<=24)
This gives you four equal-form equations of greater than equals, which we only need to enumerate s[24] to handle. You can use two-point optimization.
PS: For the a-b>=c equation, a weighted value from B to A is a forward edge of C.
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5 #defineREP (a,x) for (int a=0; a<x; a++)6 using namespacestd;7 #defineINF 0x7fffffff8 #defineMAXN 100109 structnode{Ten intV,d,next; One }EDGE[MAXN]; A inthead[ the]; - intinihead[ the]; - intE=0; the voidInit () - { -E=0; -REP (I, the) head[i]=-1; + } - voidAdd_edge (intUintVintd) + { Aedge[e].v=v; atEdge[e].d=D; -edge[e].next=Head[u]; -head[u]=e; -e++; - } - intDIS[MAXN]; in intVIS[MAXN]; - intCNT[MAXN]; to intR[MAXN]; + intNUM[MAXN]; - intSPFA (intmid) { theREP (I, -) vis[i]=0; *REP (I, -) dis[i]=-INF; $REP (I, -) cnt[i]=0;Panax Notoginsengqueue<int>Q; -Q.push (0); thevis[0]=1; +cnt[0]++; Adis[0]=0; the while(!q.empty ()) + { - intx=Q.front (); $ Q.pop (); $ for(inti=head[x];i!=-1; i=edge[i].next) - { - inty=edge[i].v; the intD=edge[i].d; - if(dis[y]<dis[x]+d)Wuyi { thedis[y]=dis[x]+D; - if(!Vis[y]) Wu { - Q.push (y); Aboutvis[y]=1; $cnt[y]++; - if(cnt[y]> -)return false; - } - } A } +vis[x]=0; the } - return 1; $ the } the the intMain () the { -Ios::sync_with_stdio (false); in //freopen ("in.in", "R", stdin); the intT; thescanf"%d",&t); About while(t--) the { theREP (I, -) scanf ("%d", &r[i+1]); the intN; + intA; -scanf"%d",&n); theREP (I, -) num[i+1]=0;Bayi REP (i,n) { thescanf"%d",&a); thenum[a+1]++; - } - init (); the for(intI=1; i<= -; i++){ the if(i>7) Add_edge (I-8, I,r[i]); theAdd_edge (i,i-1,-num[i]); theAdd_edge (I-1I0); - } the intTempe=e; theREP (I, -) inihead[i]=Head[i]; the intx=0, y=N;94 intans=INF; the while(x<y) the { theE=Tempe;98 intMid= (x+y)/2; AboutREP (I, -) head[i]=Inihead[i]; - for(intI=1;i<8; i++) Add_edge (i+ -, i,r[i]-mid);101Add_edge (0, -, mid);102 if(SPFA (mid)) {103y=mid;104ans=min (mid,ans); the }106 Else{107X=mid+1;108 }109 } the if(ans>n) printf ("No solution\n");111 Elseprintf"%d\n", ans); the }113 return 0; the}
code June
poj1275/zoj1420/hdu1529 cashier Employment (differential constraint)