Ombrophobic Bovines - POJ 2391

Description

FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They have decided to put a rain siren on the farm to let them know when rain is approaching. They intend to create a rain evacuation plan so that all the cows can get to shelter before the rain begins. Weather forecasting is not always correct, though. In order to minimize false alarms, they want to sound the siren as late as possible while still giving enough time for all the cows to get to some shelter.

The farm has F (1 <= F <= 200) fields on which the cows graze. A set of P (1 <= P <= 1500) paths connects them. The paths are wide, so that any number of cows can traverse a path in either direction.

Some of the farm's fields have rain shelters under which the cows can shield themselves. These shelters are of limited size, so a single shelter might not be able to hold all the cows. Fields are small compared to the paths and require no time for cows to traverse.

Compute the minimum amount of time before rain starts that the siren must be sounded so that every cow can get to some shelter.

Input

* Line 1: Two space-separated integers: F and P

* Lines 2..F+1: Two space-separated integers that describe a field. The first integer (range: 0..1000) is the number of cows in that field. The second integer (range: 0..1000) is the number of cows the shelter in that field can hold. Line i+1 describes field i.

* Lines F+2..F+P+1: Three space-separated integers that describe a path. The first and second integers (both range 1..F) tell the fields connected by the path. The third integer (range: 1..1,000,000,000) is how long any cow takes to traverse it.

Output

* Line 1: The minimum amount of time required for all cows to get under a shelter, presuming they plan their routes optimally. If it not possible for the all the cows to get under a shelter, output "-1".

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

OUTPUT DETAILS:

In 110 time units, two cows from field 1 can get under the shelter in that field, four cows from field 1 can get under the shelter in field 2, and one cow can get to field 3 and join the cows from that field under the shelter in field 3. Although there are other plans that will get all the cows under a shelter, none will do it in fewer than 110 time units.
 
 
题目大意
有F个牛棚,和P条道路
每个牛棚能容纳的牛的数量不同,我们要在最短的时间内移动牛,使得所有的牛都能被容纳
 
先floyd出最短路
然后二分时间+网络流判定
  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.
View Code

 

你可能感兴趣的:(poj)