链接:http://poj.org/problem?id=2391

题解:

二分答案,变成判定性问题,只需把能经过的边在当前图中联通

另外对牛和牛棚要拆点

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define ll long long
const int maxn=1e6;
const int maxn2=;
#define INF 1e16
#define INF2 1e9
int n,m,s,t,l,sum,ax;
int head[maxn2],head2[maxn2],d[maxn2],cow[maxn2],cap[maxn2];
ll dis[maxn2][maxn2];
bool vis[maxn2];
struct re{
int a,b,c,flow;
}a[maxn];
void arr(int x,int y,int z)
{
a[++l].a=head[x];
ax=max(ax,l);
a[l].b=y;
a[l].c=z;
a[l].flow=;
head[x]=l;
}
queue<int> q;
bool bfs(){
memset(vis,,sizeof(vis));
q.push(s);
d[s]=; vis[s]=;
while (!q.empty())
{
int x=q.front();q.pop();
int u=head[x];
while (u)
{
int v=a[u].b;
if (!vis[v]&&a[u].c>a[u].flow)
{
vis[v]=;
d[v]=d[x]+;
q.push(v);
}
u=a[u].a;
}
}
return(vis[t]);
}
int dfs(int x,int y)
{
if (x==t||y==) return y;
int flow=,f,tmp;
int u=head[x];
while (u)
{
int v=a[u].b;
if (d[x]+==d[v]&&(f=dfs(v,min(y,a[u].c-a[u].flow)))>)
{
a[u].flow+=f;
if (u%) tmp=u+; else tmp=u-;
a[tmp].flow-=f;
flow+=f;
y-=f;
if (y==) break;
}
u=a[u].a;
}
head[x]=u;
return(flow);
}
int maxflow()
{
int flow=;
while (bfs())
{
flow+=dfs(s,INF2);
memcpy(head,head2,sizeof(head2));
}
return(flow);
}
void floyd()
{
for (int k=;k<=n;k++)
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (i!=j)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
void buildedge(ll x)
{
memset(head,,sizeof(head));l=;
for (int i=;i<=n;i++)
{
arr(,i,cow[i]); arr(i,,);
arr(i+n,n*+,cap[i]); arr(n*+,i+n,);
arr(i,i+n,INF2); arr(i+n,i,);
}
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (dis[i][j]<=x)
{
arr(i,j+n,INF2); arr(j+n,i,);
}
}
ll solve()
{
ll ans=-,h=,t=INF-;
while (h<=t)
{
ll mid=(h+t)/;
buildedge(mid); memcpy(head2,head,sizeof(head));
if (maxflow()>=sum)
{
ans=mid; t=mid-;
} else h=mid+;
}
return ans;
}
int main()
{
freopen("noip.in","r",stdin);
freopen("noip.out","w",stdout);
std::ios::sync_with_stdio(false);
while (cin>>n>>m)
{
sum=; s=; t=*n+;
for (int i=;i<=n;i++)
{
cin>>cow[i]>>cap[i];
sum+=cow[i];
}
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
dis[i][j]=INF;
ll a,b,c;
for (int i=;i<=m;i++)
{
cin>>a>>b>>c;
dis[a][b]=min(dis[a][b],c);
dis[b][a]=min(dis[b][a],c);
}
floyd();
cout<<solve()<<endl;
}
return ;
}

poj2391的更多相关文章

  1. poj2391,poj2455

    这两题本质是一致的: 一般来说,对于最长(短)化最短(长)的问题我们一般都使用二分答案+判定是否可行 因为这样的问题,我们一旦知道答案,就能知道全局信息 拿poj2455举例,对于二分出的一个答案,我 ...

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

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

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

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

  4. ACM/ICPC 之 网络流-拆点构图(POJ2391)

    需要直接到达,因此源点经过三条边后必须要达到汇点,但为了保证网络流的正确性(路径可反悔),因此不可限制层次网络的最高层次为3,最好的方法既是让所有点拆分成两个点,一个点从汇点进入,一个点通向汇点,任意 ...

  5. poj2391 Ombrophobic Bovines 题解

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

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

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

  7. POJ2391 Ombrophobic Bovines

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

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

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

  9. poj2391 最大流+拆点

    题意:F块草坪,上面有n头牛,可以容纳m个牛遮雨.将草坪一份为2,成为二部图. 对于此题,和poj2112很像,只是2112很明显的二部图.这道题就开始敲,但是建图遇到问题,草坪的2个值怎么处理,于是 ...

随机推荐

  1. python 爆破

    python 爆破 #!/usr/bin/python #-*- coding: GB2312 -*- #author:loversorry import urllib2 import urllib ...

  2. 【51nod】1239 欧拉函数之和 杜教筛

    [题意]给定n,求Σφ(i),n<=10^10. [算法]杜教筛 [题解] 定义$s(n)=\sum_{i=1}^{n}\varphi(i)$ 杜教筛$\sum_{i=1}^{n}(\varph ...

  3. edge box

    先介绍一下matlab与c混合编程 主要步骤: 使用c语言编写函数 利用mexFunction()函数创建C与matlab接口 从Matlab中编译函数 # include <mex.h> ...

  4. .net 事务处理

    方法1:直接写入到sql 中在存储过程中使用 BEGIN TRANS, COMMIT TRANS, ROLLBACK TRANS 实现begin transdeclare @orderDetailsE ...

  5. Java SE之反射技术[Field](二)

    如果对于反射的基本概念还不了解的请见上一帖子.本文仅谈fields的用法demo /** * * @author Zen Johnny * */ package com.cpms.test; impo ...

  6. luogu P4568 [JLOI2011]飞行路线

    传送门 看到免费次数\(k\)最多只有10,可以考虑构建\(k+1\)层的分层图,即每一层正常连边,上下两层对应点连边权为0的单向边,最后对所有层里面的\(di_t\)取\(\max\)救星了 #in ...

  7. CF949D Curfew

    传送门 跟这个大佬学的->戳我 假设只有一个宿管,那么从前往后做的过程中,如果能到达某个寝室范围内的人数不够\(b\),那么不如把这个寝室空出来,这样更有利于后面的抉择;反之,就把这个寝室搞正好 ...

  8. 第18月第19天 masonry等分 uilabel sizetofit

    1.masonry等分 mas_distributeViewsAlongAxis MASAxisTypeHorizontal 2.uilabel sizetofit +(CGSize)labSizeW ...

  9. mysql 查询优化 ~explain解读之type的解读

    一 简介:今天咱们来聊聊explain中type的相关解读 二 类型: system: 表中只有一条数据. 这个类型是特殊的 const 类型.  const: 针对主键或唯一索引的等值查询扫描, 最 ...

  10. 笔记软件 notion

    笔记软件 notion :     https://www.notion.so 注册:zengxinle@126.com     团队:Hopesun