poj 2391 Ombrophobic Bovines,

最大流, 拆点, 二分

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的更多相关文章

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

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

  2. POJ 2391 Ombrophobic Bovines(Floyd+二分+最大流)

    题目链接 题意:农场有F(1 <= F <= 200)片草地用于放牛,这些草地有P(1 <= P <= 1500)连接,农场的草地上有一些避雨点,奶牛们可以在避雨点避雨,但是避 ...

  3. POJ 2391 Ombrophobic Bovines

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

  4. POJ 2391 Ombrophobic Bovines (Floyd + Dinic +二分)

    Ombrophobic Bovines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11651   Accepted: 2 ...

  5. POJ 2391 Ombrophobic Bovines(二分+拆点+最大流)

    http://poj.org/problem?id=2391 题意: 给定一个无向图,点i处有Ai头牛,点i处的牛棚能容纳Bi头牛,求一个最短时间T,使得在T时间内所有的牛都能进到某一牛棚里去. 思路 ...

  6. POJ 2391 Ombrophobic Bovines ★(Floyd+二分+拆点+最大流)

    [题意]有n块草地,一些奶牛在草地上吃草,草地间有m条路,一些草地上有避雨点,每个避雨点能容纳的奶牛是有限的,给出通过每条路的时间,问最少需要多少时间能让所有奶牛进入一个避雨点. 和POJ2112很类 ...

  7. poj 2391 Ombrophobic Bovines 最短路 二分 最大流 拆点

    题目链接 题意 有\(n\)个牛棚,每个牛棚初始有\(a_i\)头牛,最后能容纳\(b_i\)头牛.有\(m\)条道路,边权为走这段路所需花费的时间.问最少需要多少时间能让所有的牛都有牛棚可待? 思路 ...

  8. POJ 2391 Ombrophobic Bovines ( 经典最大流 && Floyd && 二分 && 拆点建图)

    题意 : 给出一些牛棚,每个牛棚都原本都有一些牛但是每个牛棚可以容纳的牛都是有限的,现在给出一些路与路的花费和牛棚拥有的牛和可以容纳牛的数量,要求最短能在多少时间内使得每头牛都有安身的牛棚.( 这里注 ...

  9. POJ 2391.Ombrophobic Bovines (最大流)

    实际上是求最短的避雨时间. 首先将每个点拆成两个,一个连接源点,一个连接汇点,连接源点的点的容量为当前单的奶牛数,连接汇点的点为能容纳的奶牛数. floyd求任意两点互相到达的最短时间,二分最长时间, ...

随机推荐

  1. 多线程--Task,等待用户输入AutoResetEvent

    上一篇文章:.NET:如何让线程支持超时?已经说明目前微软主推的多线程方案是task: 注意:Task最好引用.NET4.5. 4.0也行,但不成熟.Thread引用2.0就够了. 1.通过构造函数创 ...

  2. 【hibernate】错误:org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was altered from xx to xx

    所报错误: org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was ...

  3. 在 Ubuntu 14.04 上安装 Ubuntu Tweak 0.8.8

    转自:http://linux.cn/article-3335-1.html 关于 Ubuntu Tweak,Ubuntu 老用户再熟悉不过了,Ubuntu tweak 相当于 windows 的优化 ...

  4. dl,dt,dd标记在网页中要充分利用

    dl,dt,dd标记在网页中要充分利用 来源:网络整理 时间:08-05-27 点击: 点击这里收藏本文 我们在制作网页过程中用到列表时一般会使用<ul>或者<ol>标签,很少 ...

  5. POJ 1844 Sum【简单数学】

    链接: http://poj.org/problem?id=1844 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29256#probl ...

  6. [Functional Programming ADT] Create State ADT Based Reducers (applyTo, Maybe)

    The typical Redux Reducer is function that takes in the previous state and an action and uses a swit ...

  7. EffectiveJava(29)优先考虑类型安全的异构容器

    当你的泛型集合需要更多的灵活性,你可以将键进行参数化而不是将容器进行参数化.然后将参数化的键提交给容器,来插入或者获取值.用泛型系统来确保值得类型与它的键相符. 我们创建一个Favorite类来模拟这 ...

  8. Node.js mm131图片批量下载爬虫1.01 增加断点续传功能

    这里的断点续传不是文件下载时的断点续传,而是指在爬行页面时有时会遇到各种网络中断而从中断前的页面及其数据继续爬行的过程,这个过程和断点续传原理上相似故以此命名.我的具体做法是:在下载出现故障或是图片已 ...

  9. Notepad++的使用

    \t 制表符.  \n 新行.  . 匹配任意字符.  | 匹配表达式左边和右边的字符. 例如, "ab|bc" 匹配 "ab" 或者 "bc&quo ...

  10. [Erlang]Erlang经常使用工具解说

    原创文章,转载请注明出处:服务器非业余研究http://blog.csdn.net/erlib 作者Sunface 联系邮箱:cto@188.com 但凡有图形界面的都须要linux系统安装了wx图形 ...