BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛 网络流 + 二分 + Floyd


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.

    约翰的牛们非常害怕淋雨,那会使他们瑟瑟发抖.他们打算安装一个下雨报警器,并且安排了一个撤退计划.他们需要计算最少的让所有牛进入雨棚的时间.    牛们在农场的F(1≤F≤200)个田地上吃草.有P(1≤P≤1500)条双向路连接着这些田地.路很宽,无限量的牛可以通过.田地上有雨棚,雨棚有一定的容量,牛们可以瞬间从这块田地进入这块田地上的雨棚    请计算最少的时间,让每只牛都进入雨棚.


* 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.



* 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".

题解: 预处理出任意两点间最短距离,每次二分一下时间,时间小于等于二分的时间就进行加边. 对于每一个牛群连接容量为牛数量的边,并从雨棚向终点连边,容量为雨棚容量. 如果达到漫流,则继续往小了二分,否则向更大二分. 
#define setIO(s) freopen(s".in","r",stdin) 
#define maxn 1000000 
#define inf 10000000000000 
#define ll long long 
using namespace std;
namespace Dinic{
    struct Edge{
        int from,to,cap; 
        Edge(int u=0,int v=0,int c=0):from(u),to(v),cap(c){} 
    int vis[500],d[500],curr[500]; 
    int s,t; 
    void addedge(int u,int v,int c){
        int m=edges.size();
    int BFS(){
        d[s]=0,vis[s]=1, Q.push(s); 
            int u=Q.front();Q.pop(); 
            for(int sz=G[u].size(),i=0;i<sz;++i){
                Edge r=edges[G[u][i]]; 
                if(!vis[]&&r.cap>0) {
        return vis[t]; 
    int dfs(int x,int cur){
        if(x==t) return cur;
        int f,flow=0;
        for(int sz=G[x].size(),i=curr[x];i<sz;++i){
            Edge r=edges[G[x][i]]; 
            if(cur<=0) break; 
        return flow; 
    int maxflow(){
        int flow=0;
        while(BFS()) memset(curr,0,sizeof(curr)),flow+=dfs(s,10000000); 
        return flow; 
    void re(){
        for(int i=0;i<500;++i) G[i].clear(); 
#define row1(i) (i) 
#define row2(i) (i+n) 
int C[maxn],num[maxn],sums=0,n; 
long long d[500][500]; 
bool check(ll tmp)
	int s=0,t=row2(n+1); 
	for(int i=1;i<=n;++i) 
		if(num[i]) Dinic::addedge(s,row1(i),num[i]);
		if(C[i]) Dinic::addedge(row2(i),t,C[i]);   
	for(int i=1;i<=n;++i) 
		for(int j=1;j<=n;++j)
			if(i!=j && d[i][j]<=tmp) Dinic::addedge(row1(i), row2(j), 10000000); 
	return Dinic::maxflow() >= sums; 
int main()
	// setIO("input"); 
	int m;
	for(int i=1;i<=n;++i) scanf("%d%d",&num[i],&C[i]),sums+=num[i];  
	for(int i=0;i<=230;++i) 
	    for(int j=0;j<=230;++j) d[i][j]=inf; 
	for(int i=0;i<=230;++i) d[i][i]=0; 
	for(int i=1;i<=m;++i)
		int u,v;
		ll c;  
		if(u!=v) d[u][v]=d[v][u]=min(d[u][v],c);        
	for(int k=1;k<=n;++k) 
		for(int i=1;i<=n;++i) 
			for(int j=1;j<=n;++j) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); 
	ll l=0,r=100000000000000,ans=-1;  
    	ll mid=(l+r)>>1;
    	if(check(mid)) ans=mid,r=mid-1;
    	else l=mid+1; 
	return 0; 


posted @ 2019-06-04 11:04  EM-LGH  阅读(78)  评论(0编辑  收藏