POJ 2391 Ombrophobic Bovines (Floyd + Dinic +二分)

时间:2022-07-15 20:49:54
Ombrophobic Bovines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11651   Accepted: 2586

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.

Source

 
题意:有n块田地,已知每块田地上面牛的数量和雨篷能遮蔽的牛的数量;有m路无向边连接任意两块田地,每条路有固定的长度。问如果下雨了,所有的牛要怎么走,才能使得在最短的时间(最后的牛进入雨篷)内让所有的牛进入雨篷,如果不能的话输出-1。
 
思路:二分答案+网络流判定。先用floyd求出任意两块田地之间的最短距离,然后二分答案,用网络流判定。网络流的建图:需要拆点,源点[0]连一条权为牛数量的边到各点(in),各点(in)连一条权为INF的边到(out),各点(out)连一条权为雨篷遮蔽数量的边到汇点[2*n+1],然后符合条件的路(u,v)在点u(in)连一条权为INF的边到v(out)。这样建图求出来的最大流就是在当前时间内,有多少只牛能找到雨篷避雨了。另外有些地方要用到long long,要注意下。
 
#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 ;
}