Description
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
* 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
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
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.
//
建图把i拆为x,y;
起点到x连线 容量为开始在该点的牛数量
y到终点连线 容量为结束改点可以有多少牛
x到y if x==y,连线 容量为无限大;
else 如果x可以到达y而且
然后路劲长度小于后者等于mid 表示该边可用
才连线 容量无限大;
#include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<cstring>
using namespace std;
const long long inf=(1LL<<58);//不要开到(1<<30)
const int point_num=550;//300不够
long long cap[point_num][point_num],dist[point_num],gap[point_num];//初始化见main里面
int s0,t0,n;//源,汇和点数
long long find_path(int p,long long limit=inf)
{
if(p==t0) return limit;
for(int i=0;i<n;i++)
if(dist[p]==dist[i]+1 && cap[p][i]>0)
{
long long t=find_path(i,min(cap[p][i],limit));
if(t<0) return t;
if(t>0)
{
cap[p][i]-=t;
cap[i][p]+=t;
return t;
}
}
int label=n;
for(int i=0;i<n;i++) if(cap[p][i]>0) label=min((long long)label,dist[i]+1);
if(--gap[dist[p]]==0 || dist[s0]>=n ) return -1;
++gap[dist[p]=label];
return 0;
}
long long sap()
{
//初始化s,t
s0=0,t0=n-1;
long long t=0,maxflow=0;
gap[0]=n;
while((t=find_path(s0))>=0) maxflow+=t;
return maxflow;
}
long long d[550][550];
int x[550],y[550];
int main()
{
int m;
while(scanf("%d%d",&n,&m)==2)
{
int total=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x[i],&y[i]);
total+=x[i];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++) d[i][j]=inf;
d[i][i]=0;
}
for(int l=0;l<m;l++)
{
int u,v,w;scanf("%d%d%d",&u,&v,&w);
d[u][v]=d[v][u]=min(d[u][v],(long long)w);
}
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(d[i][j]>d[i][k]+d[k][j])
d[i][j]=d[i][k]+d[k][j];
}
}
}
int flag=0;
long long l=0,r=500000000000;//r不能直接等于inf 否则超时
while(l<r)
{
long long mid=(l+r)>>1;
//初始化
memset(cap,0,sizeof(cap));
memset(dist,0,sizeof(dist));
memset(gap,0,sizeof(gap));
//初始化cap
for(int i=1;i<=n;i++)
{
cap[0][i]=x[i];
cap[i+n][n+n+1]=y[i];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(d[i][j]<=mid) cap[i][j+n]=inf;
}
}
int t=n;
n=n*2+2;
int cnt=sap();
n=t;
if(cnt==total)
{
flag=1;r=mid;
}
else l=mid+1;
}
if(flag) printf("%I64d\n",r);
else printf("-1\n");
}
return 0;
}