跟pku2112差不多,神奇的二分答案,这个需要拆点。 program pku_2391; const inf=10000000; var p,o,tc,pre,other,c:array[0..100000] of longint; last,late,d,num:array[0..1000] of longint; a:array[0..402,0..402] of int64; limit:int64; n,m,l,st,ed,cow,r:longint; {-----------------------------} procedure grow(x,y,z:longint); begin inc(l); pre[l]:=last[x];last[x]:=l;other[l]:=y;c[l]:=z; inc(l); pre[l]:=last[y];last[y]:=l;other[l]:=x;c[l]:=0; end; {-----------------------------} function min(x,y:longint):longint; begin if x<y then exit(x);exit(y); end; {-----------------------------} function dfs(u,flow:longint):longint; var i,t:longint; begin if u=ed then exit(flow); dfs:=0; i:=last[u]; while i<>0 do begin if (c[i]>0) and (d[u]=d[other[i]]+1) then begin t:=dfs(other[i],min(flow-dfs,c[i])); dec(c[i],t); if odd(i) then inc(c[i+1],t) else inc(c[i-1],t); inc(dfs,t); if dfs=flow then exit(dfs); end; i:=pre[i]; end; if d[st]=n*2+2 then exit; dec(num[d[u]]); if num[d[u]]=0 then d[st]:=n*2+2; inc(d[u]); inc(num[d[u]]); end; {-----------------------------} function sap(limit:int64):longint; var i,j,flow,ans:longint; begin fillchar(d,sizeof(d),0); fillchar(num,sizeof(num),0); last:=late; pre:=p; other:=o; c:=tc; l:=r; for i:=1 to n do for j:=1 to n do if (i<>j) and (a[i,j]<=limit) then grow(i*2-1,2*j,inf); num[0]:=n*2+2; ans:=0; while d[st]<n*2+2 do begin flow:=dfs(st,maxlongint); inc(ans,flow); end; exit(ans); end; {-----------------------------} procedure init; var i,j,k,x,y,z:longint; begin fillchar(last,sizeof(last),0); readln(n,m); for i:=1 to n do for j:=1 to n do a[i,j]:=1000000000000000; l:=0; st:=n*2+1;ed:=st+1; limit:=0; cow:=0; for i:=1 to n do begin readln(x,y); inc(cow,x); grow(st,i*2-1,x); grow(i*2,ed,y); end; for i:=1 to n do grow(i*2-1,i*2,inf); for i:=1 to m do begin readln(x,y,z); inc(limit,z); if z<a[x,y] then begin a[x,y]:=z; a[y,x]:=z; end; end; for k:=1 to n do for i:=1 to n do for j:=1 to n do if (i<>j) and (i<>k) and (j<>k) then if a[i,k]+a[k,j]<a[i,j] then a[i,j]:=a[i,k]+a[k,j]; tc:=c; p:=pre; o:=other; late:=last; r:=l; end; {-----------------------------} procedure main; var l,r,mid:int64; begin l:=0;r:=limit; repeat mid:=(l+r) div 2; if sap(mid)=cow then r:=mid-1 else l:=mid+1; until l>r; if sap(l)=cow then writeln(l) else writeln(-1); end; {-----------------------------} begin init; main; end.