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 ;
}

POJ 2391 Ombrophobic Bovines (Floyd + Dinic +二分)的更多相关文章

  1. POJ 2391 Ombrophobic Bovines ★(Floyd+二分+拆点+最大流)

    [题意]有n块草地,一些奶牛在草地上吃草,草地间有m条路,一些草地上有避雨点,每个避雨点能容纳的奶牛是有限的,给出通过每条路的时间,问最少需要多少时间能让所有奶牛进入一个避雨点. 和POJ2112很类 ...

  2. poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点

    题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...

  3. poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap

    poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...

  4. poj 2391 Ombrophobic Bovines(最大流+floyd+二分)

    Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 14519Accepted: 3170 De ...

  5. POJ 2391 Ombrophobic Bovines

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18623   Accepted: 4 ...

  6. POJ 2391 Ombrophobic Bovines (二分答案+floyd+最大流)

    <题目链接> 题目大意: 给定一个有$n$个顶点和$m$条边的无向图,点$i$ 处有$A_i$头牛,点$i$ 处的牛棚能容纳$B_i$头牛,每条边有一个时间花费$t_i$(表示从一个端点走 ...

  7. POJ 2391 Ombrophobic Bovines(Floyd+二分+最大流)

    题目链接 题意:农场有F(1 <= F <= 200)片草地用于放牛,这些草地有P(1 <= P <= 1500)连接,农场的草地上有一些避雨点,奶牛们可以在避雨点避雨,但是避 ...

  8. POJ 2391 Ombrophobic Bovines ( 经典最大流 && Floyd && 二分 && 拆点建图)

    题意 : 给出一些牛棚,每个牛棚都原本都有一些牛但是每个牛棚可以容纳的牛都是有限的,现在给出一些路与路的花费和牛棚拥有的牛和可以容纳牛的数量,要求最短能在多少时间内使得每头牛都有安身的牛棚.( 这里注 ...

  9. POJ 2391 Ombrophobic Bovines(二分+拆点+最大流)

    http://poj.org/problem?id=2391 题意: 给定一个无向图,点i处有Ai头牛,点i处的牛棚能容纳Bi头牛,求一个最短时间T,使得在T时间内所有的牛都能进到某一牛棚里去. 思路 ...

随机推荐

  1. FreeType的项目总是报error LNK2019: unresolved external symbol __imp错误

    用vs2013建立了一个c++的项目,然后在根目录放置了freetype.lib,将GitHub上面的include文件夹拷贝到本机,并且在VS中设置了额外包含目录指向这个inluce文件夹,然后将f ...

  2. 如何将.NET 4.0写的Windows service和Web API部署到docker上面

    Web API. 看这篇文章: https://docs.microsoft.com/en-us/aspnet/mvc/overview/deployment/docker-aspnetmvc Win ...

  3. Mongoose vs mongodb native driver – what to prefer?

      Paul Shan 7th Jun 2015 Mongoose or mongodb native driver, which one to use? This is one of the ini ...

  4. CreateFont函数为什么改变不了字体?该怎么解决

    CreateFont函数为什么改变不了字体?CFont   *   f;             f   =   new   CFont;             f-> CreateFont( ...

  5. php获取网址

    #测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<br> ...

  6. OSX下安装VMware虚拟机, 加载kali系统

    准备 当前环境:OSX 10.11.6 , 准备VMware虚拟机软件和kali系统 为什么要安装kali系统 Kali Linux预装了许多渗透测试软件,包括nmap (端口扫描器).Wiresha ...

  7. gzcms技术开发文档

    1.输出统一的json格式ajaxJSON.cs: 2.web.config注册html控件:3.gzcms.contrls开发控件库:4.form序列化提交$(this).serializeJson ...

  8. 保存html代码

    function svcode(F) { if (document.all) { var F = $id(F); var E = window.open("", "_bl ...

  9. php 字符串中的\n换行符无效、不能换行的解决方法

    php 字符串中的\n换行符无效.不能换行的解决方法 程序的中的换行符\n会直接输出,无法正确换行,解决方法是把单引号改为双引号 aa

  10. Word2007的自动插入题注!

    要在word中插入图片,是截屏获取的图片,还未保存成位图.要使用word2007的自动插入题注时,发现选项里面没有关于word图片的选项,2003里面有个word图片(差不多是这个叫法吧),在2003 ...