先预处理出来每个点对之间的最短距离

然后二分答案,网络流判断是否可行就好了恩

 /**************************************************************
Problem: 1738
User: rausen
Language: C++
Result: Accepted
Time:404 ms
Memory:9788 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll;
const int N = ;
const int M = N * N << ;
const int inf_int = 1e9;
const ll inf_ll = (ll) 1e18; inline int read(); struct edge {
int next, to, f;
edge(int _n = , int _t = , int _f = ) : next(_n), to(_t), f(_f) {}
} e[M]; int n, m, S, T;
int a[N], b[N], sum;
ll mp[N][N];
int first[N], tot;
int d[N]; inline void Add_Edges(int x, int y, int z) {
e[++tot] = edge(first[x], y, z), first[x] = tot;
e[++tot] = edge(first[y], x), first[y] = tot;
}
#define y e[x].to
#define p q[l]
bool bfs() {
static int l, r, x, q[N];
memset(d, -, sizeof(d));
d[q[] = S] = ;
for (l = r = ; l != r + ; ++l)
for (x = first[p]; x; x = e[x].next)
if (!~d[y] && e[x].f) {
d[q[++r] = y] = d[p] + ;
if (y == T) return ;
}
return ;
}
#undef p int dfs(int p, int lim) {
if (p == T || !lim) return lim;
int x, tmp, rest = lim;
for (x = first[p]; x && rest; x = e[x].next)
if (d[y] == d[p] + && ((tmp = min(e[x].f, rest)) > )) {
rest -= (tmp = dfs(y, tmp));
e[x].f -= tmp, e[x ^ ].f += tmp;
if (!rest) return lim;
}
if (rest) d[p] = -;
return lim - rest;
}
#undef y int Dinic() {
int res = ;
while (bfs())
res += dfs(S, inf_int);
return res;
} inline bool check(ll t) {
static int i, j;
memset(first, , sizeof(first)), tot = ;
for (i = ; i <= n; ++i)
Add_Edges(S, i * - , a[i]), Add_Edges(i * , T, b[i]);
for (i = ; i <= n; ++i)
for (j = ; j <= n; ++j)
if (mp[i][j] <= t) Add_Edges(i * - , j * , inf_int);
return Dinic() == sum;
} int main() {
int i, j, k, x, y, z;
ll l, r, mid;
n = read(), m = read(), S = n * + , T = S + ;
for (i = ; i <= n; ++i)
sum += (a[i] = read()), b[i] = read();
for (i = ; i <= n; ++i)
for (j = ; j <= n; ++j) mp[i][j] = i == j ? : inf_ll;
for (i = ; i <= m; ++i) {
x = read(), y = read(), z = read();
if (mp[x][y] > z) mp[x][y] = mp[y][x] = z;
}
for (k = ; k <= n; ++k)
for (i = ; i <= n; ++i)
for (j = ; j <= n; ++j)
mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]); l = , r = inf_ll;
while (l + < r) {
mid = l + r >> ;
if (check(mid)) r = mid;
else l = mid;
}
printf("%lld\n", r == inf_ll ? -1ll : r);
return ;
} inline int read() {
static int x;
static char ch;
x = , ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
}

BZOJ1738 [Usaco2005 mar]Ombrophobic Bovines 发抖的牛的更多相关文章

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

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

  2. 【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 ...

  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. POJ 2391 Ombrophobic Bovines

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

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

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

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

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

  9. 1740: [Usaco2005 mar]Yogurt factory 奶酪工厂

    1740: [Usaco2005 mar]Yogurt factory 奶酪工厂 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 119  Solved:  ...

随机推荐

  1. 让WinForm窗体的大小固定,不能调整大小

    窗体FormBorderStyle属性设置为:FixedSingle,再把最大化禁用就可以了

  2. s表达式和json表达式

    s表达式 + 1 2 3普通表达式 1+2+3json表达式{ +:[1, 2, 3]}优点,一个运算符,无限个参数 s表达式 * (+ 1 2) 3普通表达式 1+(2*3)json表达式{ *:[ ...

  3. jQuery mobile 前台设计中的css em的使用。

    先贴个网址:http://www.uml.org.cn/html/201207311.asp 大家可以参考一下面这张转换表(body字体为16px时的值) 总结 长篇介绍了一大堆,唯一想告诉大家的是以 ...

  4. HDU4801·二阶魔方

    题意:给定二阶魔方初始状态,问N(1 <= N <= 7)步旋转操作以内最多能使几个面相同. dfs搜索+剪枝. 魔方的每个旋转操作即对应于一个置换操作.又因为相对运动,上层左旋一次和下层 ...

  5. mysql delimiter

    默认情况下,mysql遇到分号; 就认为是一个命令的终止符, 就会执行命令.而有些时候,我们不希望这样,比如存储过程中包含多个语句,这些语句以分号分割,我们希望这些语句作为一个命令,一起执行,怎么解决 ...

  6. 安装64位版Oracle11gR2后无法启动SQLDeveloper的解决方案(原创) (2016-10-29 下午01:56)

    安装64位版Oracle11gR2后发现启动SQL Developer时弹出配置java.exe的路径,找到Oracle自带java.exe后产生的路径"C:\app\用户名\product ...

  7. Java中ArrayDeque,栈与队列

    package ch8; import java.util.*; /** * Created by Jiqing on 2016/11/27. */ public class ArrayDequeSt ...

  8. hdu 2604 Queuing(dp递推)

    昨晚搞的第二道矩阵快速幂,一开始我还想直接套个矩阵上去(原谅哥模板题做多了),后来看清楚题意后觉得有点像之前做的数位dp的水题,于是就用数位dp的方法去分析,推了好一会总算推出它的递推关系式了(还是菜 ...

  9. 转:SQL的内连接与外连接

    参考:http://www.cuiyongjian.com/post-130.html 在oracle的SQL语句常用的连接有内连接(inner join),外连接(outer join)等,内连接又 ...

  10. 数据存储之SQLite

    SQLite是目前主流的嵌入式关系型数据库,其最主要的特点就是轻量级.跨平台,当前很多嵌入式操作系统都将其作为数据库首选.虽然SQLite是一款轻型数据库,但是其功能也绝不亚于很多大型关系数据库.学习 ...