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: ...
随机推荐
- SQL——连接查询
以mysql为例: 新建两张表table1和table2 CREATE TABLE `table1` ( `id` ) NOT NULL auto_increment, `name` ) defaul ...
- 百度翻译word-wrap,页面错乱原因查找过程(已修复)
今天群里有人发问, 进入百度翻译http://fanyi.baidu.com/#auto/zh/, 输入word-wrap,发现页面错乱. 寻找错乱原因. 上图 开始查找原因: 1.从请求入手 从ch ...
- 转 。。。。一个不规则的按钮 虽然已经不适用于cocos2dx3.0以上版本 but思路就应该是这个样子滴
本篇文章主要讲一下怎么做一个不规则的按钮,比如如下图的八卦,点击绿色和点击红色部分,需要执行不同的事件
- offsetLeft与style.left区别
在javascript中经常遇到style.left和offsetLeft,那么它们有什么区别呢?今天我们来分析下 offsetLeft:获取当前元素相对于父元素的左侧偏移量,比如该元素设置为rela ...
- CentOS6.4_x86_开关机查看
1. 有时候,开机进系统的时候,就会卡在哪里,进不去系统.查看开机时 是哪个东西卡在哪里: 开机,显示进度条的时候,按 F4,就可以看到加载的具体情况了. 2. 关机的时候,有时候关闭了机: 用 CT ...
- 【51nod】1376 最长递增子序列的数量
数组A包含N个整数(可能包含相同的值).设S为A的子序列且S中的元素是递增的,则S为A的递增子序列.如果S的长度是所有递增子序列中最长的,则称S为A的最长递增子序列(LIS).A的LIS可能有很多个. ...
- u盘在电脑读不出来,但别的可以读,别的u盘在我电脑又可以识别怎么回事?
不知道我的U盘是怎么回事,在我自己的电脑里读不出来,下面有U盘图标,但我的电脑里就是找不到U盘盘符,但把这个U盘放其他电脑上又可以读取,我以为是我的电脑的问题,但用其他的U盘插我电脑又没问题,完全摸不 ...
- 杨辉三角 && 鸽兔同校
杨辉三角: 用个一维数组直接模拟就行,只是 C++ 的高精度调了好久,后来发现能用 python ,于是试着写了写: dp = [] def out(L, end): for i in range(e ...
- 转:为什么C++中空类和空结构体大小为1?
参考:http://www.spongeliu.com/260.html 为什么C++中空类和空结构体大小为1? On November 17, 2010, in C语言, 语言学习, by spon ...
- entity reference在views中的运用
一个views block可以获取从url过来的nid,某个node的有entity reference字段, 填入一点数据,然后在views里关联一下这个entity reference field ...