BZOJ1738 [Usaco2005 mar]Ombrophobic Bovines 发抖的牛
先预处理出来每个点对之间的最短距离
然后二分答案,网络流判断是否可行就好了恩
/**************************************************************
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 发抖的牛的更多相关文章
- BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )
一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...
- 【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 ...
- BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛
Description 约翰的牛们非常害怕淋雨,那会使他们瑟瑟发抖.他们打算安装一个下雨报警器,并且安排了一个撤退计划.他们需要计算最少的让所有牛进入雨棚的时间. 牛们在农场的F(1≤F≤200 ...
- 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 ...
- bzoj 1738 [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 最大流+二分
题目要求所有牛都去避雨的最长时间最小. 显然需要二分 二分之后考虑如何判定. 显然每头牛都可以去某个地方 但是前提是最短路径<=mid. 依靠二分出来的东西建图.可以发现这是一个匹配问题 din ...
- POJ 2391 Ombrophobic Bovines
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 4 ...
- poj 2391 Ombrophobic Bovines(最大流+floyd+二分)
Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 14519Accepted: 3170 De ...
- BZOJ 1739: [Usaco2005 mar]Space Elevator 太空电梯
题目 1739: [Usaco2005 mar]Space Elevator 太空电梯 Time Limit: 5 Sec Memory Limit: 64 MB Description The c ...
- 1740: [Usaco2005 mar]Yogurt factory 奶酪工厂
1740: [Usaco2005 mar]Yogurt factory 奶酪工厂 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 119 Solved: ...
随机推荐
- windows上配置git
windows上配置git1.下载mysisigit进入http://msysgit.github.io/,下载,安装,下一步下一步即可. 2.下载tortoisegit进入http://downlo ...
- More Effective C++ (2)
接下来的是more effective c++ 11至20条款: 11.禁止异常信息(exceptions)传递到析构函数外.析构函数的调用情况可能有两种:(1)对象正常销毁 (2)异常传播过程中的栈 ...
- 改变bootstarp图标水平方向
我一开始是以为bootstarp已经自定义了方向的类的,但是我查阅了好久都没有看到,我这里用的是CSS3的旋转180° 1:HTML <i class="icon-thumbs-dow ...
- SQL server数据类型、增删改查
数据类型: 整数型:bigint.int.smallint.mediumint.tinyint 小数类型:decimal.numeric 浮点型:real.float.double 位型:bit 字符 ...
- 学习笔记day6:CSS3动画属性
总结: 1: CSS动画:@keyframes animation:ie10+:加-webkit前缀: animation 则是属于关键帧动画的范畴; 它本身被用来替代一些纯粹表现的javascri ...
- bootstrap 手风琴效果
<!DOCTYPE HTML> <html><head><meta charset="utf-8"><title>按钮插 ...
- 11 Indexes
本章提要--------------------------------------索引会影响 DML 与 select 操作, 要找到平衡点最好从一开始就创建好索引索引概述B*索引其他一些索引索引使 ...
- linux内核的熵池
也可以看百度科 Linux内核采用熵来描述数据的随机性.熵(entropy)是描述系统混乱无序程度的物理量,一个系统的熵越大则说明该系统的有序性越差,即不确定性越大.在信息学中,熵被用来表征一个符号或 ...
- intersection-of-two-arrays-ii
https://leetcode.com/problems/intersection-of-two-arrays-ii/ class Solution { public: vector<int& ...
- 两个EXCEL文件对比去重
两个EXCEL文件,A里面有10000条记录,B里面有4000条记录,A的记录包含了B里面所有的记录现在如何能把A里面没有包含B的6000条记录筛选出来? 那你要把两同时打开,比如book1 book ...