题目描述

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)条双向路连接着这些田地.路很宽,无限量的牛可以通过.田地上有雨棚,雨棚有一定的容量,牛们可以瞬间从这块田地进入这块田地上的雨棚    请计算最少的时间,让每只牛都进入雨棚.

输入

* 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之间).

输出

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

样例输入

3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120

样例输出

110


题解

floyd+二分+拆点+网络流

先用floyd求出任意两点之间的距离。

然后二分答案,若i与j之间的距离小于等于mid,则将i与j'(拆出来的点)间连一条容量为正无穷的边。

将源点与每个点间连一条容量为牛数的边,将每个拆出来的点与汇点间连一条容量为牛棚容量的边。

然后跑网络流,判断是否满流即可。

注意图可以是不连通的,所以当ans过大时,说明必须要用到题目中不存在的边,即无论如何都不能满足题意,输出-1。

注意距离要开long long。

#include <cstdio>
#include <cstring>
#include <queue>
#define inf 0x3fffffff
using namespace std;
queue<int> q;
long long dis[201][201];
int a[201] , b[201] , head[403] , to[180000] , val[180000] , next[180000] , cnt , s , t , deep[403];
void add(int x , int y , long long z)
{
to[++cnt] = y;
val[cnt] = z;
next[cnt] = head[x];
head[x] = cnt;
}
bool bfs()
{
int x , i;
while(!q.empty())
q.pop();
memset(deep , 0 , sizeof(deep));
deep[s] = 1;
q.push(s);
while(!q.empty())
{
x = q.front();
q.pop();
for(i = head[x] ; i ; i = next[i])
{
if(val[i] && !deep[to[i]])
{
deep[to[i]] = deep[x] + 1;
if(to[i] == t)
return 1;
q.push(to[i]);
}
}
}
return 0;
}
int dinic(int x , int low)
{
if(x == t)
return low;
int temp = low , i , k;
for(i = head[x] ; i ; i = next[i])
{
if(val[i] && deep[to[i]] == deep[x] + 1)
{
k = dinic(to[i] , min(temp , val[i]));
if(!k) deep[to[i]] = 0;
val[i] -= k;
val[i ^ 1] += k;
if(!(temp -= k)) break;
}
}
return low - temp;
}
bool judge(int n , long long mid , int sum)
{
memset(head , 0 , sizeof(head));
memset(to , 0 , sizeof(to));
memset(val , 0 , sizeof(val));
memset(next , 0 , sizeof(next));
cnt = 1;
int i , j , maxflow = 0;
for(i = 1 ; i <= n ; i ++ )
{
add(s , i , a[i]);
add(i , s , 0);
add(i + n , t , b[i]);
add(t , i + n , 0);
for(j = 1 ; j <= n ; j ++ )
{
if(i == j || dis[i][j] <= mid)
add(i , j + n , inf) , add(j + n , i , 0);
}
}
while(bfs())
maxflow += dinic(s , inf);
return maxflow == sum;
}
int main()
{
int n , m , i , j , k , x , y , suma = 0 , sumb = 0;
long long z , l = 0 , r = 0 , mid , ans = -1;
scanf("%d%d" , &n , &m);
s = 0 , t = 2 * n + 1;
for(i = 1 ; i <= n ; i ++ )
scanf("%d%d" , &a[i] , &b[i]) , suma += a[i] , sumb += b[i];
memset(dis , 0x3f , sizeof(dis));
for(i = 1 ; i <= m ; i ++ )
scanf("%d%d%lld" , &x , &y , &z) , dis[x][y] = dis[y][x] = min(dis[x][y] , z);
if(suma > sumb)
{
printf("-1\n");
return 0;
}
for(k = 1 ; k <= n ; k ++ )
for(i = 1 ; i <= n ; i ++ )
for(j = 1 ; j <= n ; j ++ )
dis[i][j] = min(dis[i][j] , dis[i][k] + dis[k][j]);
for(i = 1 ; i <= n ; i ++ )
for(j = 1 ; j <= n ; j ++ )
if(i != j)
r = max(r , dis[i][j]);
while(l <= r)
{
mid = (l + r) >> 1;
if(judge(n , mid , suma))
ans = mid , r = mid - 1;
else
l = mid + 1;
}
printf("%lld\n" , ans < 10000000000000ll ? ans : -1);
return 0;
}

【bzoj1738】[Usaco2005 mar]Ombrophobic Bovines 发抖的牛 Floyd+二分+网络流最大流的更多相关文章

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

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

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

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

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

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

  4. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 网络流 + 二分 + Floyd

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...

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

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

  6. 【bzoj1733】[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+网络流最大流

    题目描述 Farmer John is constructing a new milking machine and wishes to keep it secret as long as possi ...

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

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

  8. Ombrophobic Bovines

    poj2391:http://poj.org/problem?id=2391 题意:一个人有n个农场,每个农场都一个避雨的地方,每个农场有一些牛,每个避雨的地方能容纳牛的数量是有限的.农场之间有一些道 ...

  9. POJ 2391 Ombrophobic Bovines

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

随机推荐

  1. Java设计模式(14)——行为模式之不变模式(Immutable)

    一.概述 概念 分类:弱不变模式(子类可变)和强不变模式(子类也是不可变) 应用场景 java.lang.String是一个经典的强不变类 二.分析 与享元模式的关系

  2. katalon系列六:Katalon Studio Web UI关键字讲解

    在一个Test Case里,点左上Add-Web UI Keyword,可以添加一行新的命令. 像Click.setText.Delay这些最基本的,大家还是看看官方的API文档吧,望文知义,如果是纯 ...

  3. Java开发工程师(Web方向) - 02.Servlet技术 - 期末考试

    Servlet课程考试 Servlet课程考试 Servlet课程考试 总分:55分 限定时间:120分钟 进入考试 答案已成功提交!请耐心等待成绩公布 Servlet课程考试: 1(12分) 简单谈 ...

  4. 编写你自己的Python模块

    其实网上Python教程挺多的,编写你自己的模块很简单,这其实就是你一直在做的事情!这是因为每一个 Python 程序同时也是一个模块.你只需要保证它以 .py 为扩展名即可.下面的案例会作出清晰的解 ...

  5. python 终极篇 ---django 认证

    Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Djang ...

  6. leetcode-岛屿的个数

    岛屿的个数 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 ...

  7. Java学习 · 初识 面向对象基础一

    面向对象基础 1.1面向过程与面向对象的区别 面向过程和面向对象二者都是思考问题的方式,再简单的事物时,可以线性思考时使用面向过程,但当事物较为复杂时,只能使用面向对象设计.但二者并不是对立的,在解决 ...

  8. 干货来袭:Redis5.0支持的新功能说明

    Redis5.0支持的新特性说明 本文内容来自华为云帮助中心 华为云DCS的Redis5.x版本继承了4.x版本的所有功能增强以及新的命令,同时还兼容开源Redis5.x版本的新增特性. Stream ...

  9. vmware安装64位系统“此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态”的问题

    虚拟机使用的是VMware Workstation,并且首次在虚拟机体验64 位系统.在新建好虚拟机,运行时候就出现了VMware Workstation 的提醒:此主机支持 Intel VT-x,但 ...

  10. 冥冥中转到了mac 上进行开发

    2013年愚人节前我的开发环境情况 我是一个有着15年windows使用经历的老programer,如果算上dos那还可以加两年.当过小企业网管,做过十二年的开发工作(直到老死,~_~).这期间当然也 ...