【poj2391 floyd+拆点+最大流+二分】Ombrophobic Bovines

Ombrophobic Bovines

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22104 Accepted: 4746

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个点和P条路径,其中F点处有n头牛,并且该点可以使m头牛防止被雨淋湿。P条路径为双向边,可以容纳无限头牛行走,求让所有牛不淋湿的话,牛最远需要走多久

首先二分最远距离,每一次二分后重新建图

需要对点进行拆分,因为每一次建边的时候我们建了一个容量为INF的边,如果a->b=10,b->c=20,a->c=1000,假设当前二分的距离为50,我们需要建立a->b  b->c两条边,如果不对点进行拆分的话,相当于a->c也连上了一条容量为INF的边,所以此时会WA

另外:

1、题有重边,注意距离取最小值

2、使用floyd时,该题距离会超过int,所以使用longlong。但是不要初始化为0X3f3f3f3f,因为有可能距离会超过这个数(一般0x3f3f3f3f用于int类型,因为其二倍刚好不会超过int),所以此题中初始化为1LL<<60,注意不要忘记LL

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<iostream>
#include<queue>
using namespace std;
#define ll long long
const int MAXN = 100010;//点数的最大值
const int MAXM = 400010;//边数的最大值
int a[100010],b[100010];
ll dis[5000][5000];
const int INF = 0x3f3f3f3f;
struct Edge
{
    int to,next,cap,flow;
} edge[MAXM]; //注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];
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].flow = 0;
    edge[tol].next = head[u];
    head[u] = tol++;
    edge[tol].to = u;
    edge[tol].cap = rw;
    edge[tol].flow = 0;
    edge[tol].next = head[v];
    head[v] = tol++;
}
int Q[MAXN];
void BFS(int start,int end)
{
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0] = 1;
    int front = 0, rear = 0;
    dep[end] = 0;
    Q[rear++] = end;
    while(front != rear)
    {
        int u = Q[front++];
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to;
            if(dep[v] != -1)continue;
            Q[rear++] = v;
            dep[v] = dep[u] + 1;
            gap[dep[v]]++;
        }
    }
}
int S[MAXN];
int sap(int start,int end,int N)
{
    BFS(start,end);
    memcpy(cur,head,sizeof(head));
    int top = 0;
    int u = start;
    int ans = 0;
    while(dep[start] < N)
    {
        if(u == end)
        {
            int Min = INF;
            int inser;
            for(int i = 0; i < top; i++)
                if(Min > edge[S[i]].cap - edge[S[i]].flow)
                {
                    Min = edge[S[i]].cap - edge[S[i]].flow;
                    inser = i;
                }
            for(int i = 0; i < top; i++)
            {
                edge[S[i]].flow += Min;
                edge[S[i]^1].flow -= Min;
            }
            ans += Min;
            top = inser;
            u = edge[S[top]^1].to;
            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] = i;
                break;
            }
        }
        if(flag)
        {
            S[top++] = cur[u];
            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[S[--top]^1].to;
    }
    return ans;
}

int n,m;
void build(ll nowdis){
    init();
    for(ll i=1;i<=n;i++){
        addedge(1,i*2,a[i]);
        addedge(i*2+1,2*n+2,b[i]);
        addedge(i*2,i*2+1,INF);
    }
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n;j++){
            if(dis[i][j]<=nowdis){
                addedge(i*2,j*2+1,INF);
                addedge(j*2,i*2+1,INF);
            }
        }
    }
}
int main(){
    while(~scanf("%d%d",&n,&m)){
        int maxnum=0;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&a[i],&b[i]);
            maxnum+=a[i];
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                dis[i][j]= i==j?0:(1LL<<60);
        ll l=0,r=0;
        for(int i=1;i<=m;i++){
            int aa,bb;
            ll cc;
            scanf("%d%d%lld",&aa,&bb,&cc);
            dis[aa][bb]=dis[bb][aa]=min(cc,dis[aa][bb]);
        }
        for(ll k=1;k<=n;k++){
            for(ll i=1;i<=n;i++){
                for(ll j=1;j<=n;j++){
                    dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
                }
            }
        }
        for(ll i=1;i<=n;i++){
            for(ll j=1;j<=n;j++){
                if(dis[i][j]<(1LL<<60))
                    r=max(r,dis[i][j]);
            }
        }
        ll temp;
        init();
        build(r);
        if(sap(1,2*n+2,2*n+2)<maxnum){
            printf("-1\n");
        }
        else{
            while(l<r){
                ll mid=(l+r)/2;
                init();
                build(mid);
                temp=sap(1,2*n+2,2*n+2);
                if(temp>=maxnum){
                    r=mid;
                }
                else{
                    l=mid+1;
                }
            }
            printf("%lld\n",r);
        }
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值