题解见【网络流总结】 这里只放代码算了 program syj; const oo=maxlongint>>1; var n,m,s,t,x,y,z,i,kk,e,ans,nn,flow:longint;ok:boolean; d,h,a,b,sl,fir,pre:array[0..1005]of longint; next,point,w,c:array[-15005..15005]of longint; q:array[1..200000]of longint; v:array[0..1005]of boolean; procedure link(x,y,z,cc:longint); begin inc(e); next[e]:=h[x];point[e]:=y;w[e]:=z;c[e]:=cc;h[x]:=e; next[-e]:=h[y];point[-e]:=x;w[-e]:=0;c[-e]:=-cc;h[y]:=-e; end; procedure imp; // dist lable + many road improving (zkw+MRI) var i,j:longint; begin i:=t;j:=oo; while i<>s do begin if w[fir[i]]<=j then begin j:=w[fir[i]];kk:=pre[i]; end; i:=pre[i]; end; i:=t; while i<>s do begin dec(w[fir[i]],j);inc(w[-fir[i]],j); i:=pre[i]; end; inc(ans,(b[t]-b[s])*j); inc(flow,j); end; procedure dfs(i:longint); var j,k,z:longint; begin if i=t then begin imp;ok:=true;exit; end; v[i]:=true; j:=h[i]; while j<>0 do begin k:=point[j]; if (w[j]>0)and(c[j]+b[i]-b[k]<sl[k]) then sl[k]:=c[j]+b[i]-b[k]; if (w[j]>0)and(c[j]+b[i]=b[k])and not v[k] then begin pre[k]:=i;fir[k]:=j; dfs(k); if ok then begin if i=kk then ok:=false else exit; end; end; j:=next[j]; end; end; function find:boolean; var i,z:longint; begin z:=oo; for i:=0 to nn do if not v[i]and(sl[i]<z) then z:=sl[i]; if z=oo then exit(false); for i:=0 to nn do if v[i] then dec(b[i],z); find:=true; end; begin assign(input,'employee.in');reset(input); assign(output,'employee.out');rewrite(output); readln(n,m); s:=0;t:=n+2; for i:=1 to n do read(a[i]); for i:=1 to n+1 do if a[i]-a[i-1]>=0 then link(s,i,a[i]-a[i-1],0) else link(i,t,a[i-1]-a[i],0); for i:=1 to n do link(i+1,i,oo,0); for i:=1 to m do begin readln(x,y,z); link(x,y+1,oo,z); end; nn:=n+2; while find do begin fillchar(sl,sizeof(sl),127); repeat fillchar(v,sizeof(v),0);ok:=false; dfs(s); until not ok; end; writeln(ans); close(input);close(output); end.