POJ 2391 最大流 二分 拆点 floyd


Ombrophobic Bovines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16756 Accepted: 3656

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.


题意是有F个牛棚。里面现在有多少只牛 但是最多能容纳多少只牛  所以其他牛需要去别的棚子

棚子和棚子之间是无向图。。。用floyd一遍就搞定了

这道题。。不知道怎么回事。。无脑RE 重写到第三遍的时候。才AC 的。。BUT 。。TELL MY WHY。。。



#include <iostream>  
#include <algorithm>  
#include <cstring>  
#include <string>  
#include <cstdio>  
#include <cmath>    
  
using namespace std;  
const int MAXN = 100010 ; //点数最大值    
const int MAXM = 400010 ; //边数最大值    
const int INF = 0x3f3f3f3f;    
const long long int LLINF=1e16;

int S,V,F,P,ISUM;  
struct Edge{    
    int to,next,cap,flow;    
}edge[MAXM];//注意是MAXM    
int tol;    
int head[MAXN];    
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];    
long long int imap[555][555],IMAX;
int aa[555],bb[555]; 
void floyd(){  
    int i,j,k;  
    for(k=1;k<=F;k++){  
        for(i=1;i<=F;i++){  
            for(j=1;j<=F;j++){  
                imap[i][j]=imap[i][j]<(imap[i][k]+imap[k][j])?imap[i][j]:(imap[i][k]+imap[k][j]);
            }  
        }  
    } 
    //printf("%I64d",imap[1][20]);
/*
	printf("测试输出\n"); 
    for(i=1;i<=F;i++){ 
        for(j=1;j<=F;j++){ 
            printf("%I64d  ",imap[i][j]); 
        } 
        printf("\n"); 
    }*/ 
}  
  
void init(){    
    tol = 0;    
    memset(head,-1,sizeof(head));    
}     
  
void addedge(int u,int v,int w,int rw=0){    
    edge[tol].to = v;    
    edge[tol].cap = w;    
    edge[tol].next = head[u];    
    edge[tol].flow = 0;    
    head[u] = tol++;    
    edge[tol].to = u;    
    edge[tol].cap = rw;    
    edge[tol].next = head[v];    
    edge[tol].flow = 0;    
    head[v] = tol++;    
}    
  //最大流开始   
int sap(int start,int end,int N){    
    memset(gap,0,sizeof(gap));    
    memset(dep,0,sizeof(dep));    
    memcpy(cur,head,sizeof(head));    
    int u = start;    
    pre[u] = -1;    
    gap[0] = N;    
    int ans = 0;    
    while(dep[start] < N){    
        if(u==end){    
            int Min = INF;    
            for(int i=pre[u];i!= -1; i=pre[edge[i^1].to])    
                if(Min > edge[i].cap - edge[i].flow)    
                    Min = edge[i].cap - edge[i].flow;    
                        
            for(int i=pre[u];i!=-1;i=pre[edge[i^1].to]){    
                edge[i].flow += Min;    
                edge[i^1].flow -=Min;    
            }    
            u=start;    
            ans +=Min;    
            continue;    
        }    
        bool flag = false;    
        int v;    
        for(int i= cur[u];i!=-1;i=edge[i].next){    
            v=edge[i].to;    
            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]){    
                flag=true;    
                cur[u]=pre[v]=i;    
                break;    
            }    
        }    
        if(flag){    
            u=v;    
            continue;    
        }    
        int Min = N;    
        for(int i=head[u];i!= -1;i=edge[i].next)    
            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min){    
                Min=dep[edge[i].to];    
                cur[u] = i;    
            }    
        gap[dep[u]]--;    
        if(!gap[dep[u]]) return ans;    
        dep[u] = Min +1;    
        gap[dep[u]]++;    
        if(u!=start) u = edge[pre[u]^1].to;    
    }    
    return ans;    
}    
//最大流结束   

bool build(long long int mid){
	int i,j,k,l,m,n;
	init();
	
	for(i=1;i<=F;i++){
		addedge(S,i,aa[i]);
		addedge(i+F,V,bb[i]);
	}
	for(i=1;i<=F;i++)
		for(j=1;j<=F;j++){
			if(imap[i][j]<=mid)	addedge(i,j+F,INF);
		}
	
	int orz=sap(S,V,V+1);
	return orz==ISUM;
	
}



long long int solve(){  
    int i,j,k;
	for(i=1;i<=F;i++){  
    	for(j=1;j<=F;j++){  
       		if(imap[i][j]!=LLINF){
        		IMAX=IMAX>imap[i][j]?IMAX:imap[i][j];
          	}
		}  
  	}  
    long long int l=0,r=IMAX,mid,ans=-1;
    
    while(r>=l){
    	mid=(l+r)>>1;
    	if(build(mid)){
	    	r=mid-1;
	    	ans=mid;
	    }
	    else{
    		l=mid+1;
    	}
    }
    return ans;
    
}  
  
int main(){    
    int m,n,q,p;    
    int i,j,k,a,b,c; 
	for(i=0;i<555;i++){
		for(j=0;j<555;j++){
			if(i==j)	imap[i][j]=0;
			else imap[i][j]=LLINF;
		}
	}
	IMAX=0;
    scanf("%d%d",&F,&P);
	S=0;
	V=2*F+1; 
	ISUM=0;
  	for(i=1;i<=F;i++){
	  	scanf("%d%d",&aa[i],&bb[i]);
	  	ISUM+=aa[i];
	}
	for(i=1;i<=P;i++){
		scanf("%d%d%d",&a,&b,&c);
		if(imap[a][b]>c){
			imap[a][b]=imap[b][a]=c;
		}
	}
	floyd();
	long long int orz=solve();
	printf("%lld\n",orz);
	return 0;
}    




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值