Interval and be sure to contact the prefix and.
- This problem, the prefix and as a point, from S0 to SN;
- For each camp I capacity capi, there is such a relationship si-si-1<=capi;
- For each interval of evaluation i,j,k, there is sj-si-1>=k, i.e. si-1-sJ<=k;
- Next is the edge, for v<=u+w from u to v Lian Quan W, the super-source to n+1 points of the edge 0.
- finally run SPFA, if there is negative ring there is no solution; and there is a solution, the answer is in D[SN], but because the title of the prefix and the non-negative integer and the minimum requirements, it is necessary to let all the numbers in D at the same time with the appropriate number to get another real solution.
1#include <cstdio>2#include <cstring>3#include <queue>4#include <algorithm>5 using namespacestd;6 #defineINF (1<<30)7 #defineMAXN 11118 #defineMAXM 222229 Ten structedge{ One intV,cost,next; A }EDGE[MAXM]; - intHead[maxn],ne; - voidAddedge (intUintVintCost ) { theEdge[ne].v=v; Edge[ne].cost=cost; edge[ne].next=Head[u]; -head[u]=ne++; - } - + intN,M,D[MAXN],CNT[MAXN]; - BOOLVIS[MAXN]; + BOOLSPFA () { A for(intI=0; i<=n; ++i) { atvis[i]=0; cnt[i]=0; d[i]=INF; - } -vis[n+1]=1; cnt[n+1]=1; d[n+1]=0; -queue<int>que; -Que.push (n+1); - while(!Que.empty ()) { in intu=Que.front (); Que.pop (); - if(cnt[u]>n+1)return 0; to for(intI=head[u]; i!=-1; I=Edge[i].next) { + intv=edge[i].v; - if(d[v]>d[u]+edge[i].cost) { thed[v]=d[u]+Edge[i].cost; * if(!Vis[v]) { $vis[v]=1;Panax Notoginseng++Cnt[v]; - Que.push (v); the } + } A } thevis[u]=0; + } - return 1; $ } $ intMain () { - intA,B,C,CAP[MAXN]; - while(~SCANF ("%d%d",&n,&m)) { the for(intI=1; i<=n; ++i) scanf ("%d", cap+i); - Wuyimemset (head,-1,sizeof(head)); theNe=0; - for(intI=1; i<=n; ++i) Addedge (i-1, I,cap[i]); Wu for(intI=0; i<=n; ++i) Addedge (n+1I0); - while(m--){ Aboutscanf"%d%d%d",&a,&b,&c); $Addedge (b,a-1,-c); - } - if(SPFA ()) { - inttmp=INF; A for(intI=0; i<=n; ++i) tmp=min (tmp,d[i]); + if(tmp<0) d[n]-=tmp; theprintf"%d\n", D[n]); -}ElsePuts"Bad estimations"); $ } the return 0; the}
ZOJ2770 Burn the Linked Camp (differential constraint system)