Description
Input
Output
Sample Input
3 4 7 2 0 4 2 6 1 2 40 3 2 70 2 3 90 1 3 120
Sample Output
110
Hint
1 const 2 maxn=220; 3 inf=10000000; 4 var 5 first,now,pre,vh,dis,his:array[0..maxn*2]of longint; 6 f:array[0..maxn,0..maxn]of int64; 7 last,next,liu:array[0..maxn*maxn*20]of longint; 8 a,b:array[0..maxn]of longint; 9 n,m,sum,tot:longint; 10 11 procedure insert(x,y,z:longint); 12 begin 13 inc(tot);last[tot]:=y;next[tot]:=first[x];first[x]:=tot;liu[tot]:=z; 14 inc(tot);last[tot]:=x;next[tot]:=first[y];first[y]:=tot;liu[tot]:=0; 15 end; 16 17 procedure down(var x:int64;y:int64); 18 begin 19 if x>y then x:=y; 20 end; 21 22 function flow:longint; 23 var 24 i,j,jl,min,aug:longint; 25 flag:boolean; 26 begin 27 for i:=0 to n<<1+1 do now[i]:=first[i]; 28 for i:=0 to n<<1+1 do vh[i]:=0; 29 for i:=0 to n<<1+1 do dis[i]:=0; 30 vh[0]:=n<<1+2;flow:=0; 31 i:=0;aug:=inf; 32 while dis[i]<n<<1+2 do 33 begin 34 his[i]:=aug; 35 flag:=false; 36 j:=now[i]; 37 while j<>0 do 38 begin 39 if (liu[j]>0) and (dis[i]=dis[last[j]]+1) then 40 begin 41 if aug>liu[j] then aug:=liu[j]; 42 now[i]:=j; 43 pre[last[j]]:=j; 44 i:=last[j]; 45 flag:=true; 46 if i=n<<1+1 then 47 begin 48 inc(flow,aug); 49 while i<>0 do 50 begin 51 dec(liu[pre[i]],aug); 52 inc(liu[pre[i]xor 1],aug); 53 i:=last[pre[i]xor 1]; 54 end; 55 aug:=inf; 56 end; 57 break; 58 end; 59 j:=next[j]; 60 end; 61 if flag then continue; 62 min:=n<<1+1; 63 j:=first[i]; 64 while j<>0 do 65 begin 66 if (liu[j]>0) and (dis[last[j]]<min) then 67 begin 68 min:=dis[last[j]]; 69 jl:=j; 70 end; 71 j:=next[j]; 72 end; 73 dec(vh[dis[i]]); 74 if vh[dis[i]]=0 then break; 75 now[i]:=jl; 76 dis[i]:=min+1; 77 inc(vh[min+1]); 78 if i<>0 then 79 begin 80 i:=last[pre[i]xor 1]; 81 aug:=his[i]; 82 end; 83 end; 84 end; 85 86 procedure main; 87 var 88 i,j,k,x,y:longint; 89 l,r,z,mid,max:int64; 90 begin 91 fillchar(f,sizeof(f),1); 92 read(n,m); 93 for i:=1 to n do read(a[i],b[i]); 94 for i:=1 to n do inc(sum,a[i]); 95 for i:=1 to n do f[i,i]:=0; 96 for i:=1 to m do 97 begin 98 read(x,y,z); 99 if z<f[x,y] then 100 begin 101 f[x,y]:=z; 102 f[y,x]:=z; 103 end; 104 end; 105 for k:=1 to n do 106 for i:=1 to n do 107 for j:=1 to n do 108 down(f[i,j],f[i,k]+f[k,j]); 109 r:=0; 110 for i:=1 to n do 111 for j:=1 to n do 112 if (r<f[i,j]) and (f[i,j]<f[0,0]) then r:=f[i,j]; 113 l:=0;max:=r;inc(r); 114 while l<>r do 115 begin 116 mid:=(l+r)>>1; 117 tot:=1; 118 for i:=0 to n<<1+1 do first[i]:=0; 119 for i:=1 to n do insert(0,i,a[i]); 120 for i:=1 to n do insert(i+n,n<<1+1,b[i]); 121 for i:=1 to n do 122 for j:=1 to n do 123 if f[i,j]<=mid then insert(i,j+n,inf); 124 if flow>=sum then r:=mid 125 else l:=mid+1; 126 end; 127 if l>max then writeln(-1) 128 else writeln(l); 129 end; 130 131 begin 132 main; 133 end.