实际上是求最短的避雨时间。

首先将每个点拆成两个,一个连接源点,一个连接汇点,连接源点的点的容量为当前单的奶牛数,连接汇点的点为能容纳的奶牛数。

floyd求任意两点互相到达的最短时间,二分最长时间,最大流判断是否可行。

注意路径时间会超过int

/*
最大流SAP
邻接表
思路:基本源于FF方法,给每个顶点设定层次标号,和允许弧。
优化:
1、当前弧优化(重要)。
1、每找到以条增广路回退到断点(常数优化)。
2、层次出现断层,无法得到新流(重要)。
时间复杂度(m*n^2)
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#define ms(a,b) memset(a,b,sizeof a)
using namespace std;
const int INF = ;
long long G[INF][INF];
struct node {
int v, c, next;
} edge[INF*INF * ];
long long pHead[INF*INF], SS, ST, nCnt;
void addEdge (int u, int v, int c) {
edge[++nCnt].v = v; edge[nCnt].c = c, edge[nCnt].next = pHead[u]; pHead[u] = nCnt;
edge[++nCnt].v = u; edge[nCnt].c = , edge[nCnt].next = pHead[v]; pHead[v] = nCnt;
}
int SAP (int pStart, int pEnd, int N) {
int numh[INF], h[INF], curEdge[INF], pre[INF];
int cur_flow, flow_ans = , u, neck, i, tmp;
ms (h, ); ms (numh, ); ms (pre, -);
for (i = ; i <= N; i++) curEdge[i] = pHead[i];
numh[] = N;
u = pStart;
while (h[pStart] <= N) {
if (u == pEnd) {
cur_flow = 1e9;
for (i = pStart; i != pEnd; i = edge[curEdge[i]].v)
if (cur_flow > edge[curEdge[i]].c) neck = i, cur_flow = edge[curEdge[i]].c;
for (i = pStart; i != pEnd; i = edge[curEdge[i]].v) {
tmp = curEdge[i];
edge[tmp].c -= cur_flow, edge[tmp ^ ].c += cur_flow;
}
flow_ans += cur_flow;
u = neck;
}
for ( i = curEdge[u]; i != ; i = edge[i].next)
if (edge[i].c && h[u] == h[edge[i].v] + ) break;
if (i != ) {
curEdge[u] = i, pre[edge[i].v] = u;
u = edge[i].v;
}
else {
if ( == --numh[h[u]]) continue;
curEdge[u] = pHead[u];
for (tmp = N, i = pHead[u]; i != ; i = edge[i].next)
if (edge[i].c) tmp = min (tmp, h[edge[i].v]);
h[u] = tmp + ;
++numh[h[u]];
if (u != pStart) u = pre[u];
}
}
return flow_ans;
}
long long m, n, x, y,sum,c;
int in[INF], out[INF];
bool check (long long tem) {
nCnt = ;
SS = * n + , ST = * n + ;
memset (pHead, , sizeof pHead);
for (int i = ; i <= n; i++) {
if(out[i]) addEdge (SS, i, out[i]);
for (int j =; j <= n; j++)
if (G[i][j] <= tem&&G[i][j]!=-)
addEdge (i, j+n, );
}
for (int i = ; i <= n; i++)
if(in[i])addEdge (i + n, ST, in[i]);
int ans = SAP (SS, ST, ST);
if (ans == sum) return ;
return ;
}
int main() {
/*
建图,前向星存边,表头在pHead[],边计数 nCnt.
SS,ST分别为源点和汇点
*/
ms (G, -);
cin>>n>>m;
for (int i = ; i <= n; i++) {
cin>>out[i]>>in[i];
sum += out[i];
}
long long l = 0x7fffffffffffffff, r = ;
for (int i = ; i <= n; i++) G[i][i] = ;
for (int i = ; i <= m; i++) {
cin>>x>>y>>c;
if(G[x][y]>) G[x][y] = G[y][x] = min(c,G[x][y]);
else
G[x][y]=G[y][x]=c;
l = min (l, c), r = max (r, c);
}
for (int t = ; t <= n; t++)
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++) {
if (G[i][t] == - || G[t][j] == -) continue;
if (G[i][j] == - || G[i][j] > G[i][t] + G[t][j])
G[i][j] = G[i][t] + G[t][j], l = min (l, G[i][j]), r = max (r, G[i][j]);
}
long long last = -,mid;
while (l <= r) {
mid = (l + r) >> ;
if (check (mid) ) {
last = mid;
r = mid - ;
}
else l = mid + ;
}
cout<<last;
return ;
}

POJ 2391.Ombrophobic Bovines (最大流)的更多相关文章

  1. poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分, dinic, isap

    poj 2391 Ombrophobic Bovines, 最大流, 拆点, 二分 dinic /* * Author: yew1eb * Created Time: 2014年10月31日 星期五 ...

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

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

  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 (二分答案+floyd+最大流)

    <题目链接> 题目大意: 给定一个有$n$个顶点和$m$条边的无向图,点$i$ 处有$A_i$头牛,点$i$ 处的牛棚能容纳$B_i$头牛,每条边有一个时间花费$t_i$(表示从一个端点走 ...

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

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

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

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

随机推荐

  1. (转载)INSERT INTO .. ON DUPLICATE KEY 语法与实例教程

    (转载)http://www.111cn.net/database/mysql/ON_DUPLICATE_KEY%20.htm INSERT语句末尾指定了ON DUPLICATE KEY UPDATE ...

  2. PHP中Content-type的MIME类型大全说明

    <?php $mimetypes = array(         'ez' => 'application/andrew-inset', 'hqx' => 'application ...

  3. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. 第二十三章、软件安装: RPM, SRPM 与 YUM 功能

    SRPM 的使用 : rpmbuild 包含Source code 的 SRPM 新版的 rpm 已经将 RPM 与 SRPM 的命令分开了,SRPM 使用的是 rpmbuild 这个命令,而不是 r ...

  5. wiki 的捐款呼吁有感而发

    今早在维基百科上查东西时,网页的最上角出现了一栏小广告,我一看居然是维基创始人发的捐款呼吁,点开后网页内容如下: “     来自维基百科创办者Jimmy Wales的个人呼吁 ----------- ...

  6. 在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程)

    在浏览器中简单输入一个网址,解密其后发生的一切(http请求的详细过程) 原文链接:http://www.360doc.com/content/14/1117/10/16948208_42571794 ...

  7. 【Android - 框架】之GreenDao的使用

    上一篇博客([Android - 框架]之ORMLite的使用)中介绍了ORMLite的基本使用,今天我们来研究以下GreenDao的使用. GreenDao和ORMLite一样,都是基于ORM(Ob ...

  8. PC机安装Qt以及QT交叉编译环境 分类: OpenCV ZedBoard shell ubuntu Eye_Detection 2014-11-08 18:57 246人阅读 评论(0) 收藏

    PC: apt-get install qtcreator Qt Embedded for ZedBoard: 下载qt-everywhere-opensource-src-4.7.3.tar.gz, ...

  9. [Reactive Programming] Using an event stream of double clicks -- buffer()

    See a practical example of reactive programming in JavaScript and the DOM. Learn how to detect doubl ...

  10. android 45 通知

    package com.sxt.day07_01; import android.app.Activity; import android.app.Notification; import andro ...