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.

    约翰的牛们非常害怕淋雨,那会使他们瑟瑟发抖.他们打算安装一个下雨报警器,并且安排了一个撤退计划.他们需要计算最少的让所有牛进入雨棚的时间.    牛们在农场的F(1≤F≤200)个田地上吃草.有P(1≤P≤1500)条双向路连接着这些田地.路很宽,无限量的牛可以通过.田地上有雨棚,雨棚有一定的容量,牛们可以瞬间从这块田地进入这块田地上的雨棚    请计算最少的时间,让每只牛都进入雨棚.

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.

    第1行:两个整数F和P;
    第2到F+1行:第i+l行有两个整数描述第i个田地,第一个表示田地上的牛数,第二个表示田地上的雨棚容量.两个整数都在0和1000之间.
    第F+2到F+P+I行:每行三个整数描述一条路,分别是起点终点,及通过这条路所需的时间(在1和10^9之间).

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".

    一个整数,表示最少的时间.如果无法使牛们全部进入雨棚,输出-1.
 
题解: 预处理出任意两点间最短距离,每次二分一下时间,时间小于等于二分的时间就进行加边. 对于每一个牛群连接容量为牛数量的边,并从雨棚向终点连边,容量为雨棚容量. 如果达到漫流,则继续往小了二分,否则向更大二分. 
 
#include<bits/stdc++.h>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 1000000
#define inf 10000000000000
#define ll long long
using namespace std;
namespace Dinic{
struct Edge{
int from,to,cap;
Edge(int u=0,int v=0,int c=0):from(u),to(v),cap(c){}
};
vector<int>G[500];
vector<Edge>edges;
queue<int>Q;
int vis[500],d[500],curr[500];
int s,t;
void addedge(int u,int v,int c){
edges.push_back(Edge(u,v,c)),edges.push_back(Edge(v,u,0));
int m=edges.size();
G[u].push_back(m-2),G[v].push_back(m-1);
}
int BFS(){
memset(vis,0,sizeof(vis));
d[s]=0,vis[s]=1, Q.push(s);
while(!Q.empty()){
int u=Q.front();Q.pop();
for(int sz=G[u].size(),i=0;i<sz;++i){
Edge r=edges[G[u][i]];
if(!vis[r.to]&&r.cap>0) {
vis[r.to]=1,d[r.to]=d[u]+1;
Q.push(r.to);
}
}
}
return vis[t];
}
int dfs(int x,int cur){
if(x==t) return cur;
int f,flow=0;
for(int sz=G[x].size(),i=curr[x];i<sz;++i){
curr[x]=i;
Edge r=edges[G[x][i]];
if(d[r.to]==d[x]+1&&r.cap>0){
f=dfs(r.to,min(cur,r.cap));
cur-=f,flow+=f,edges[G[x][i]].cap-=f,edges[G[x][i]^1].cap+=f;
}
if(cur<=0) break;
}
return flow;
}
int maxflow(){
int flow=0;
while(BFS()) memset(curr,0,sizeof(curr)),flow+=dfs(s,10000000);
return flow;
}
void re(){
for(int i=0;i<500;++i) G[i].clear();
edges.clear();
}
};
#define row1(i) (i)
#define row2(i) (i+n)
int C[maxn],num[maxn],sums=0,n;
long long d[500][500];
bool check(ll tmp)
{
Dinic::re();
int s=0,t=row2(n+1);
Dinic::s=s,Dinic::t=t;
for(int i=1;i<=n;++i)
{
if(num[i]) Dinic::addedge(s,row1(i),num[i]);
if(C[i]) Dinic::addedge(row2(i),t,C[i]);
}
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
{
if(i!=j && d[i][j]<=tmp) Dinic::addedge(row1(i), row2(j), 10000000);
}
return Dinic::maxflow() >= sums;
}
int main()
{
// setIO("input");
int m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) scanf("%d%d",&num[i],&C[i]),sums+=num[i];
for(int i=0;i<=230;++i)
for(int j=0;j<=230;++j) d[i][j]=inf;
for(int i=0;i<=230;++i) d[i][i]=0;
for(int i=1;i<=m;++i)
{
int u,v;
ll c;
scanf("%d%d%lld",&u,&v,&c);
if(u!=v) d[u][v]=d[v][u]=min(d[u][v],c);
}
for(int k=1;k<=n;++k)
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j) d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
ll l=0,r=100000000000000,ans=-1;
while(l<=r)
{
ll mid=(l+r)>>1;
if(check(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
printf("%lld\n",ans);
return 0;
}

  

BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 网络流 + 二分 + Floyd的更多相关文章

  1. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

    一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...

  2. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛

    Description 约翰的牛们非常害怕淋雨,那会使他们瑟瑟发抖.他们打算安装一个下雨报警器,并且安排了一个撤退计划.他们需要计算最少的让所有牛进入雨棚的时间.    牛们在农场的F(1≤F≤200 ...

  3. bzoj 1738 [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 最大流+二分

    题目要求所有牛都去避雨的最长时间最小. 显然需要二分 二分之后考虑如何判定. 显然每头牛都可以去某个地方 但是前提是最短路径<=mid. 依靠二分出来的东西建图.可以发现这是一个匹配问题 din ...

  4. BZOJ1738 [Usaco2005 mar]Ombrophobic Bovines 发抖的牛

    先预处理出来每个点对之间的最短距离 然后二分答案,网络流判断是否可行就好了恩 /************************************************************ ...

  5. 【bzoj1738】[Usaco2005 mar]Ombrophobic Bovines 发抖的牛 Floyd+二分+网络流最大流

    题目描述 FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain m ...

  6. bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】

    二分答案,贪心判定 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...

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

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

  8. BZOJ 1739: [Usaco2005 mar]Space Elevator 太空电梯

    题目 1739: [Usaco2005 mar]Space Elevator 太空电梯 Time Limit: 5 Sec  Memory Limit: 64 MB Description The c ...

  9. BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

    最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...

随机推荐

  1. 记一个简单的webpack.config.js

    module.exports = { entry: './basic/app.js', output: { path: './assets/', filename: '[name].bundle.js ...

  2. 关于重置IOS App请求推送的授权请求

    项目要加入推送通知.測试完本地通知后.发现測不了远程通知.于是想重置授权请求. 下面是重置授权请求的方法: 方法一: 通用->还原->抹掉全部内容和设置 可是第一种方法非常费时,抹掉内容预 ...

  3. 在CentOS VPS上源代码安装高版本号git

    背景:个别软件在国内下载非常慢,在vps下载就非常快. 可是下载好后的文件通过scp弄出来的时候又非常慢,所以想通过在vps里安装git,通过gitlab或oschina来进行中转.但遗憾的是,上传到 ...

  4. Oracle学习(一):基本操作和基本查询语句

    文中以"--"开头的语句为凝视,即为绿色部分 1.知识点:能够对比以下的录屏进行阅读 SQL> --录屏工具spool,開始录制,并指定保存路径为c:\基本查询.txt SQ ...

  5. WPF学习笔记——在“System.Windows.StaticResourceExtension”上提供值时引发了异常

    在"System.Windows.StaticResourceExtension"上提供值时引发了异常 因应需要,写了一个转换器,然后窗体上引用,结果就出来这个错.编译的时候没事, ...

  6. luogu1991 无线通讯网

    题目大意 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络:每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话.任意两个配备了一条卫星电话线路的哨所(两边都ᤕ有 ...

  7. PCB MongoDB 数据库 Collection集合导出与导入

    由于一直以来用微软可视化图形界面习惯了,而MongoDB是命令式操作,而用系统自带CMD操作不方便, 这里介绍一款CMD的替代品,大小100多M. Cmder工具下载  https://github. ...

  8. css 浮动问题详解

    浮动(float),一个我们即爱又恨的属性.爱,因为通过浮动,我们能很方便地布局: 恨,浮动之后遗留下来太多的问题需要解决,特别是IE6-7(以下无特殊说明均指 windows 平台的 IE浏览器). ...

  9. 关于sklearn中的导包交叉验证问题

    机器学习sklearn中的检查验证模块: 原版本导包: from sklearn.cross_validation import cross_val_score 导包报错: 模块继承在cross_va ...

  10. CodeForces A. Meeting of Old Friends

    2019-05-30 20:19:57 加油!!! sort(a + 1, a + 5); 卡了一会儿 #include <bits/stdc++.h> using namespace s ...