Ombrophobic Bovines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19359   Accepted: 4186

Description

FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They have decided to put a rain siren on the farm to let them know when rain is approaching. They intend to create a rain evacuation plan so that all the cows can get to shelter before the rain begins. Weather forecasting is not always correct, though. In order to minimize false alarms, they want to sound the siren as late as possible while still giving enough time for all the cows to get to some shelter.

The farm has F (1 <= F <= 200) fields on which the cows graze. A set of P (1 <= P <= 1500) paths connects them. The paths are wide, so that any number of cows can traverse a path in either direction.

Some of the farm's fields have rain shelters under which the cows can shield themselves. These shelters are of limited size, so a single shelter might not be able to hold all the cows. Fields are small compared to the paths and require no time for cows to traverse.

Compute the minimum amount of time before rain starts that the siren must be sounded so that every cow can get to some shelter.

Input

* Line 1: Two space-separated integers: F and P

* Lines 2..F+1: Two space-separated integers that describe a field. The first integer (range: 0..1000) is the number of cows in that field. The second integer (range: 0..1000) is the number of cows the shelter in that field can hold. Line i+1 describes field
i.

* Lines F+2..F+P+1: Three space-separated integers that describe a path. The first and second integers (both range 1..F) tell the fields connected by the path. The third integer (range: 1..1,000,000,000) is how long any cow takes to traverse it.

Output

* Line 1: The minimum amount of time required for all cows to get under a shelter, presuming they plan their routes optimally. If it not possible for the all the cows to get under a shelter, output "-1".

Sample Input

3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120

Sample Output

110

Hint

OUTPUT DETAILS:

In 110 time units, two cows from field 1 can get under the shelter in that field, four cows from field 1 can get under the shelter in field 2, and one cow can get to field 3 and join the cows from that field under the shelter in field 3. Although there are
other plans that will get all the cows under a shelter, none will do it in fewer than 110 time units.

Source

———————————————————————————————

题目的意思是给出n个牛棚的牛数量和容量及各个牛棚间距离,求让所有牛找找碰最短距离

思路:先floyd求各个点最短距离,再二分最短距离用网络流验证,见图示要把一个牛棚拆成2个点。

别忘了long long和不行输出-1

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 500 struct node
{
int u, v, next, cap;
} edge[MAXN*MAXN];
int nt[MAXN], s[MAXN], d[MAXN], visit[MAXN];
int cnt;
int n,m,k;
LL mp[MAXN][MAXN];
int a[MAXN],b[MAXN]; void init()
{
cnt = 0;
memset(s, -1, sizeof(s));
} void add(int u, int v, int c)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cap = c;
edge[cnt].next = s[u];
s[u] = cnt++;
edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].cap = 0;
edge[cnt].next = s[v];
s[v] = cnt++;
} bool BFS(int ss, int ee)
{
memset(d, 0, sizeof d);
d[ss] = 1;
queue<int>q;
q.push(ss);
while (!q.empty())
{
int pre = q.front();
q.pop();
for (int i = s[pre]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (edge[i].cap > 0 && !d[v])
{
d[v] = d[pre] + 1;
q.push(v);
}
}
}
return d[ee];
} int DFS(int x, int exp, int ee)
{
if (x == ee||!exp) return exp;
int temp,flow=0;
for (int i = nt[x]; ~i ; i = edge[i].next, nt[x] = i)
{
int v = edge[i].v;
if (d[v] == d[x] + 1&&(temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
{
edge[i].cap -= temp;
edge[i ^ 1].cap += temp;
flow += temp;
exp -= temp;
if (!exp) break;
}
}
if (!flow) d[x] = 0;
return flow;
} int Dinic_flow(LL mid)
{
init(); for(int i=1; i<=n; i++)
add(0,i,a[i]),add(i+n,2*n+1,b[i]);
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(mp[i][j]<=mid)
add(i,j+n,INF);
int ss=0,ee=2*n+1;
int ans = 0;
while (BFS(ss, ee))
{
for (int i = 0; i <=ee; i++) nt[i] = s[i];
ans+= DFS(ss, INF, ee);
}
return ans;;
} void floyd()
{
for(int k = 1; k <= n; ++k)
{
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
}
}
}
} int main()
{
int u,v;
LL c;
while(~scanf("%d%d",&n,&m))
{ for(int i=0; i<=n; i++)
for(int j=0; j<=n; j++)
{
if(i!=j)
mp[i][j]=1e14;
else
mp[i][j]=0;
}
int sum=0;
for(int i=1; i<=n; i++)
scanf("%d%d",&a[i],&b[i]),sum+=a[i];
for(int i=0; i<m; i++)
{
scanf("%d%d%lld",&u,&v,&c);
mp[u][v]=min(c,mp[u][v]);
mp[v][u]=mp[u][v];
}
floyd();
LL l=0,r=1e13;
LL ans=-1;
while(l<=r)
{
LL mid=(l+r)/2;
if(Dinic_flow(mid)==sum) ans=mid,r=mid-1;
else l=mid+1;
}
printf("%lld\n",ans);
}
return 0;
}

  

POJ2391 Ombrophobic Bovines的更多相关文章

  1. poj2391 Ombrophobic Bovines 拆点+二分法+最大流

    /** 题目:poj2391 Ombrophobic Bovines 链接:http://poj.org/problem?id=2391 题意:有n块区域,第i块区域有ai头奶牛,以及一个可以容纳bi ...

  2. POJ2391 Ombrophobic Bovines(网络流)(拆点)

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

  3. POJ2391 Ombrophobic Bovines 网络流拆点+二分+floyed

    题目链接: id=2391">poj2391 题意: 有n块草地,每块草地上有一定数量的奶牛和一个雨棚,并给出了每一个雨棚的容(牛)量. 有m条路径连接这些草地  ,这些路径是双向的, ...

  4. poj2391 Ombrophobic Bovines 题解

    http://poj.org/problem?id=2391 floyd+网络流+二分 题意:有一个有向图,里面每个点有ai头牛,快下雨了牛要躲进雨棚里,每个点有bi个雨棚,每个雨棚只能躲1头牛.牛可 ...

  5. POJ2391:Ombrophobic Bovines(最大流+Floyd+二分)

    Ombrophobic Bovines Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 21660Accepted: 4658 题目 ...

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

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

  7. POJ 2391 Ombrophobic Bovines

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

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

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

  9. Ombrophobic Bovines 分类: POJ 图论 最短路 查找 2015-08-10 20:32 2人阅读 评论(0) 收藏

    Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16539 Accepted: 3605 ...

随机推荐

  1. Selenium+TestNG+Maven+Jenkins+SVN(转载)

    转载自:https://blog.csdn.net/u014202301/article/details/72354069 一. 创建Maven项目,下载Selenium和TestNG的依赖(依赖可以 ...

  2. [Solution] 893. Groups of Special-Equivalent Strings

    Difficulty: Easy Problem You are given an array A of strings. Two strings S and T are special-equiva ...

  3. Windows 窗体

    Windows系统,顾名思义,就是窗口系统,每一个程序都可以用窗口来展示,所以,为了展示窗口,需要多做一系列的工作,当然,也有纯控制台应用,就不用附带窗口了. 首先就是窗口程序的入口地址,与传统的in ...

  4. C++实现词法分析器

    #include <iostream> #include <stdlib.h> #include <stdio.h> using namespace std; ]= ...

  5. jmeter执行case结果插入DB生成报表和备份记录

    前言:由于通过jmeter写的接口自动化木有数据导入和统计分析功能,因此做了二次开发,目的是读取每条case获取接口名称和用例名称,通过获取的case执行结果进行计算,得到详细接口的用例通过率存入DB ...

  6. EasyPR源码剖析(9):字符识别

    在上一篇文章的介绍中,我们已经通过相应的字符分割方法,将车牌区域进行分割,得到7个分割字符图块,接下来要做的就是将字符图块放入训练好的神经网络模型,通过模型来预测每个图块所表示的具体字符.神经网络的介 ...

  7. Java Swing实现展示数据,以及过滤排序

    public class RelationCostctrTable extends DefaultTableModel { public RelationCostctrTable(Vector< ...

  8. Finance API文档

    0. 公共部分 请求url {apiRoot}/{method}?ver={version}&appkey={appkey}&sign={sign} 参数名 说明 示例 apiRoot ...

  9. appium sendkeys 输入数字丢失问题

    参考:https://blog.csdn.net/rainshine1190/article/details/82814503

  10. HttpServerUtility常用方法

    //HttpServerUtility是一个工具类,为了在后台处理请求方便获取到一些常用的类型,Asp.net将很多常用的东西封装到这里. // 获取服务器的计算机名称. public string ...