Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11651 | Accepted: 2586 |
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.
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int VM=;
const int EM=;
const int INF=0x3f3f3f3f; struct Edge{
int u,v,nxt;
int cap;
}edge[EM<<]; int n,m,cnt,head[VM],g[VM][VM],dep[VM];
int src,des,cow[VM],shelter[VM];
long long map[VM][VM]; void addedge(int cu,int cv,int cw){
edge[cnt].u=cu; edge[cnt].v=cv; edge[cnt].cap=cw;
edge[cnt].nxt=head[cu]; head[cu]=cnt++;
edge[cnt].u=cv; edge[cnt].v=cu; edge[cnt].cap=;
edge[cnt].nxt=head[cv]; head[cv]=cnt++;
} int BFS(){
queue<int> q;
while(!q.empty())
q.pop();
memset(dep,-,sizeof(dep));
dep[src]=;
q.push(src);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].v;
if(edge[i].cap> && dep[v]==-){
dep[v]=dep[u]+;
q.push(v);
}
}
}
return dep[des]!=-;
} int DFS(int u,int minx){
if(u==des)
return minx;
int tmp;
for(int i=head[u];i!=-;i=edge[i].nxt){
int v=edge[i].v;
if(edge[i].cap> && dep[v]==dep[u]+ && (tmp=DFS(v,min(minx,edge[i].cap)))){
edge[i].cap-=tmp;
edge[i^].cap+=tmp;
return tmp;
}
}
dep[u]=-;
return ;
} int Dinic(){
int ans=,tmp;
while(BFS()){
while(){
tmp=DFS(src,INF);
if(tmp==)
break;
ans+=tmp;
}
}
return ans;
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
map[i][j]=(i==j?:-);
int sum=;
for(int i=;i<=n;i++){
scanf("%d%d",&cow[i],&shelter[i]);
sum+=cow[i];
}
long long maxx=-;
int u,v,w;
while(m--){
scanf("%d%d%d",&u,&v,&w);
if(map[u][v]==- || map[u][v]>w){
map[u][v]=map[v][u]=w;
maxx=max(maxx,(long long)w);
}
}
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
if(map[i][k]==- || map[k][j]==-)
continue;
if(map[i][j]==- || map[i][k]+map[k][j]<map[i][j]){
map[i][j]=map[i][k]+map[k][j];
maxx=max(maxx,map[i][j]);
}
}
long long l=,r=maxx+,mid,ans=-;
while(l<=r){
mid=(l+r)>>;
cnt=;
memset(head,-,sizeof(head));
src=, des=*n+;
for(int i=;i<=n;i++){
addedge(src,i,cow[i]);
addedge(i,i+n,INF);
addedge(i+n,des,shelter[i]);
for(int j=;j<=n;j++)
if(i!=j && map[i][j]!=- && map[i][j]<=mid)
addedge(i,j+n,INF);
}
if(Dinic()==sum){
ans=mid;
r=mid-;
}else
l=mid+;
}
cout<<ans<<endl;
}
return ;
}