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: ...
随机推荐
- ubuntu 下解决安装包依赖问题
我们用banshee软件安装豆瓣插件来做个示例: 1.首先在Ubuntu Software Center内搜索banshee播放器 因为我之前已经安装好了,可以看到如下界面,你可以在点击该软件看到in ...
- Java中List、Collections实现梭哈游戏
package ch8; import java.util.*; /** * Created by Jiqing on 2016/11/27. */ public class ShowHand { / ...
- JavaSE复习_6 枚举类
△单例类是指只有一个实例,而枚举类实际上就是有有限个实例的类,在类里已经把实例定义好了. △枚举类的三种创建形式: 1) enum Week { MON,TUE,WED;//枚举类有默认构造函数创建的 ...
- Swift语言学习之OC和Swift混编
本文转自http://www.helloswift.com.cn/swiftbase/2015/0112/3469.html iOS OC和Swift混编 1.创建一个swift或者oc的工程:我这里 ...
- linux 命令 第一波
man 命令名字 查看命令详细解释 q退出cd 切换目录cd .. 回到上级目录su 切换用户pwd 当前目录mkdir cmy 创建cmy文件夹[目录]rm cmy 删除cmy文件夹[如果cmy里面 ...
- 每一个程序员需要了解的10个Linux命令
作为一个程序员,在软件开发职业生涯中或多或少会用到Linux系统,并且可能会使用Linux命令来检索需要的信息.本文将为各位开发者分享10个有用的Linux命令,希望对你会有所帮助. 以下就是今天我们 ...
- listToString
http://www.oschina.net/code/snippet_109648_2229
- 例题:打印正三角形。两层for循环,难点明白行与列的关系
while (true) { string s = "★";//s代表五角星 string t = ...
- linux笔记:linux常用命令-权限管理命令
一个文件的权限只有root和所有者可以更改. 权限管理命令:chmod(改变文件或目录的权限) 权限的数字表示: 用权限加减的方式改变权限(u代表所有者,g代表所属组,o代表其他人,a代表所有人): ...
- 【Problem solved】发现输入法都是仅桌面使用,无法输入中文时
你打开命令提示符输入CTFMON就可以啦.