#include <stdio.h>#include<queue>#defineMAXN 1003#defineMAXM 10002*4#defineINF 10000000using namespacestd;//start number must be minimum, end number must be maximumBOOLVIS[MAXN];//whether the record in the SPFA is in the queuestructedge{Edge*next,*op;//op is pointing to the opposite side intT,c,v;//T next point number, C capacity, V weight}ES[MAXM],*V[MAXN];//es edge static adjacency table, V-point numberintn,m,s,t,ec=-1;//s source point minimum, T meeting point Max, EC current edge numberintDEMOND[MAXN],SP[MAXN],PREV[MAXN];//record distance in SPSPFA, prev record previous point pathEdge *PATH[MAXN];//Synchronize records with Prev, record to previous edgevoidAddedge (intAintBintVintC=INF) {Edge E1={v[a],0, B,c,v},e2={v[b],0A0,-v}; es[++ec]=e1; v[a]=&Es[ec]; es[++ec]=e2; v[b]=&Es[ec]; V[a]->op=v[b]; v[b]->op=v[a];}voidinit () {inti,a,b,c;; scanf ("%d%d",&n,&M); for(intI=1; i<=n;i++) {scanf ("%d",&Demond[i]); } for(intI=1; i<=m;i++) {scanf ("%d%d%d",&a,&b,&c); Addedge (A, B+1, c); } S=0; t=n+2; for(intI=1; i<=n+1; i++) {C=demond[i]-demond[i-1]; if(c>=0) Addedge (S,i,0, c); ElseAddedge (I,t,0,-c); if(i>1) Addedge (i,i-1,0); }}BOOLSPFA () {intu,v; for(u=s;u<=t;u++) {Sp[u]=INF; } Queue<int>P; Prev[s]=-1; Q.push (S); Sp[s]=0; Vis[s]=1; while(!Q.empty ()) {u=Q.front (); Vis[u]=0; Q.pop (); for(Edge *k=v[u];k;k=k->next) {v=k->T; if(k->c>0&&sp[u]+k->v<Sp[v]) {Sp[v]=sp[u]+k->v; PREV[V]=u; PATH[V]=K; if(vis[v]==0) {Vis[v]=1; Q.push (v); } } } } returnsp[t]!=INF;}intargument () {inti,cost=inf,flow=0; Edge*e; for(i=t;prev[i]!=-1; i=Prev[i]) {e=Path[i]; if(e->c<cost) cost=e->C; } for(inti=t;prev[i]!=-1; i=Prev[i]) {e=Path[i]; E->c-=cost;e->op->c+=Cost ; Flow+=e->v*Cost ; } returnflow;}intMaxcostflow () {intflow=0; while(SPFA ()) {Flow+=argument (); } returnFlow;}intMain () {init (); printf ("%d\n", Maxcostflow ()); return 0;}
HYSBZ 1061 Volunteers recruit "minimum cost flow" "differential" "Minimum cost flow template"