poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap
最大流, 拆点, 二分
dinic
/*
* Author: yew1eb
* Created Time: 2014年10月31日 星期五 15时39分22秒
* File Name: poj2391.cpp
*/
#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
const int inf = 1e9;
const ll INF = 1e18;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int maxn = 600;
const int maxm = 100000;
struct Edge{
int to, next;
int cap;
Edge(){}
Edge(int nt, int t, int cp):next(nt),to(t),cap(cp){}
}edge[maxm]; int head[maxn], tot;
int n, m, S, T; void init(){
tot = 0;
memset(head, -1, sizeof head );
} void add(int u, int v, int c){
edge[tot] = Edge(head[u], v, c); head[u] = tot++;
edge[tot] = Edge(head[v], u, 0); head[v] = tot++;
} int d[maxn];
bool vis[maxn]; bool bfs(){
queue<int> q;
q.push(S);
memset(vis, 0, sizeof vis );
vis[S] = 1; d[S] = 0;
while(!q.empty()){
int u = q.front(); q.pop();
for(int i=head[u]; ~i; i=edge[i].next){
int &v = edge[i].to;
if(!vis[v] && edge[i].cap>0){
vis[v] = 1;
d[v] = d[u] + 1;
q.push(v);
}
}
}
return vis[T];
} int dfs(int u, int a){
if(u==T||a==0) return a;
int flow = 0, f;
for(int i=head[u];~i; i=edge[i].next){
int &v = edge[i].to;
if(d[u]+1==d[v]&&(f=dfs(v,min(a,edge[i].cap)))>0){
edge[i].cap -= f;
edge[i^1].cap += f;
flow += f;
a -= f;
if(a==0) break;
}
}
return flow;
} int dinic(){
int flow = 0;
while(bfs()){
flow += dfs(S, inf);
}
return flow;
} ll g[maxn][maxn];
void floyd(){
for(int k=1; k<=n; ++k)
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j){
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
} int a[maxn], b[maxn];
void create_graph(ll x){
init();
for(int i=1; i<=n; ++i)
for(int j=1; j<=n; ++j)
if(g[i][j]<=x) add(i,j+n, inf);
for(int i=1; i<=n; ++i){
add(S, i, a[i]);
add(i+n, T, b[i]);
}
} int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("out.cpp", "w", stdout);
#endif // ONLINE_JUDGE
scanf("%d%d", &n, &m);
int u, v, c;
S = 0, T = 2*n+2;
int sum = 0;
for(int i=1; i<=n; ++i){
scanf("%d%d", &a[i], &b[i]);
sum += a[i];
} for(int i=1; i<=n; ++i) {
g[i][i] = 0;
for(int j=i+1; j<=n; ++j)
g[i][j] = g[j][i] = INF;
}
for(int i=1; i<=m; ++i){
scanf("%d%d%d", &u, &v, &c);
if(g[u][v]>c) g[u][v] = g[v][u] = c;
}
floyd(); //bsearch
ll l = 0, r = 0, mid, ans = -1;
for(int i=1; i<=n; ++i) for(int j=1; j<=n; ++j)
if(g[i][j]<INF) r = max(r, g[i][j]);
while(l<=r){
mid = (l+r)>>1;
create_graph(mid);
int flow = dinic();
if(flow == sum){
ans = mid;
r = mid - 1;
}else
l = mid + 1;
}
cout<<ans<<endl;
return 0;
}
isap
int get_flow(int u, int flow)
{
if(u==T || flow==0)return flow;
int res=0, f;
for(int i=head[u]; ~i; i=edge[i].next) {
int &v = edge[i].to;
if(d[u]>d[v] && (f=get_flow(v,min(flow,edge[i].cap)))>0) {
edge[i].cap -= f;
edge[i^1].cap += f;
res += f;
flow -= f;
if(flow==0) return res;
}
}
if(!(--gap[d[u]]))d[S]=cnt+2;
gap[++d[u]]++;
return res;
} int isap()
{
int flow = 0;
memset(gap, 0, sizeof gap );
memset(d, 0, sizeof d );
cnt = T-S+1;//顶点数
gap[0] = cnt;
while(d[S]<cnt) flow += get_flow(S, inf);
return flow;
}
poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap的更多相关文章
- poj 2391 Ombrophobic Bovines(最大流+floyd+二分)
Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 14519Accepted: 3170 De ...
- POJ 2391 Ombrophobic Bovines(Floyd+二分+最大流)
题目链接 题意:农场有F(1 <= F <= 200)片草地用于放牛,这些草地有P(1 <= P <= 1500)连接,农场的草地上有一些避雨点,奶牛们可以在避雨点避雨,但是避 ...
- POJ 2391 Ombrophobic Bovines
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 4 ...
- POJ 2391 Ombrophobic Bovines (Floyd + Dinic +二分)
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11651 Accepted: 2 ...
- POJ 2391 Ombrophobic Bovines(二分+拆点+最大流)
http://poj.org/problem?id=2391 题意: 给定一个无向图,点i处有Ai头牛,点i处的牛棚能容纳Bi头牛,求一个最短时间T,使得在T时间内所有的牛都能进到某一牛棚里去. 思路 ...
- POJ 2391 Ombrophobic Bovines ★(Floyd+二分+拆点+最大流)
[题意]有n块草地,一些奶牛在草地上吃草,草地间有m条路,一些草地上有避雨点,每个避雨点能容纳的奶牛是有限的,给出通过每条路的时间,问最少需要多少时间能让所有奶牛进入一个避雨点. 和POJ2112很类 ...
- poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点
题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...
- POJ 2391 Ombrophobic Bovines ( 经典最大流 && Floyd && 二分 && 拆点建图)
题意 : 给出一些牛棚,每个牛棚都原本都有一些牛但是每个牛棚可以容纳的牛都是有限的,现在给出一些路与路的花费和牛棚拥有的牛和可以容纳牛的数量,要求最短能在多少时间内使得每头牛都有安身的牛棚.( 这里注 ...
- POJ 2391.Ombrophobic Bovines (最大流)
实际上是求最短的避雨时间. 首先将每个点拆成两个,一个连接源点,一个连接汇点,连接源点的点的容量为当前单的奶牛数,连接汇点的点为能容纳的奶牛数. floyd求任意两点互相到达的最短时间,二分最长时间, ...
随机推荐
- Linux下KVM的图形界面管理工具(WebVirtMgr)(Web版)
WebVirtMgr面板 截图 介绍 WebVirtMgr是一个基于libvirt的Web界面,用于管理虚拟机.它允许您创建和配置新域,并调整域的资源分配.VNC查看器为来宾域提供完整的图形控制台.K ...
- 在WPF中使用FontAwesome之类的字体图标
我之前在博客中介绍过几个矢量图库网站,在WPF程序中,一般接触到的矢量图标资源有XAML.SVG.字体这三种格式.XAML是标准格式就不说了,SVG并不是直接支持的,不过微软提供了Expression ...
- HDU 4305 Lightning(计算几何,判断点在线段上,生成树计数)
Lightning Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Java RSA加密算法生成公钥和私钥
原文:http://jingyan.baidu.com/article/6dad5075f33466a123e36ecb.html?qq-pf-to=pcqq.c2c 目前为止,RSA是应用最多的公钥 ...
- 【Todo】Boost安装与学习
现在这里找下载包 http://sourceforge.net/projects/boost 我找的是 1_62_0 下面是从公司wiki上找到的一个说明. boost & thrift安装步 ...
- Java内存缓存
1.缓存为什么要存在 应用服务器资源是有限的,数据库每秒中接受请求的次数也是有限的.如果利用有限的资源来提供尽可能大的吞吐量呢,一个办法:减少计 算量,缩短请求流程(减少网络io或者硬盘io),这时候 ...
- http://www.cnblogs.com/zhengyun_ustc/p/55solution2.html
http://www.cnblogs.com/zhengyun_ustc/p/55solution2.html http://wenku.baidu.com/link?url=P756ZrmasJTK ...
- [置顶] (奇迹冬瓜)坦克大战[MFC框架]
经过二次整合 重新放出MFC框架下的坦克大战 采用小窗口 多线程 双缓冲 动画帧化 碰撞检测 接口封装 混音 事件延迟等 力求做到代码与美工的双向化 开场动画 界面一 界面二 游戏界面 结束动画 零积 ...
- Expression-Based Access Control
Expression-Based Access Control Spring Security 3.0 introduced the ability to use Spring EL expressi ...
- Discuz常见小问题2-如何在新建的页面上只显示一部分板块
切换到论坛-版块管理,记住要只显示的板块的gid(比如我的是36) 为某个主导航设置一个单独的php页面(名字自己取) 如果这个页面内容跟首页forum.php完全一样,只是第三行增加了一句话 ...