码迷,mamicode.com
首页 > 其他好文 > 详细

Ombrophobic Bovines (poj 2391 网络流+二分+Floyd)

时间:2015-02-24 09:09:39      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:ombrophobic bovines   poj 2391   网络流+二分+floyd   

Language:
Ombrophobic Bovines
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15733   Accepted: 3434

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


题意:农场有F块草地,奶牛们在草地上吃草。这些草地间有P条路相连,这些路足够宽,再多的牛也能在路上行走。有些草地上有避雨点,奶牛在避雨点避雨。避雨点的容量是有限的。奶牛要在下雨前全部到达某个避雨点,计算报警至少要提前多少时间拉响,以保证所有的奶牛能够到达一个避雨点。

思路:先预处理floyd求出各点之间的最短路,拆点,将每个点拆成两个,注意第i个点连第i+F个点时保证单向,防止回流,权值为inf,另外是无向边,要建两条边(这个wa了我二十多发),网络流+二分,二分时间来判断两个地方能不能连边,求最大流,如果最大流>=牛的总数,则 r=mid-1;否则l=mid+1。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 855
#define MAXM 1000001
#define mod 1000000009
const int inf=1<<30;
#define INF 10000000000000
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

int tol,f,p,s,t,sum;
ll mp[MAXN][MAXN],maxxx;
int date[MAXN][2];

struct Edge
{
    int to,next,cap,flow;
}edge[MAXM];

int head[MAXN];
int gap[MAXN],dep[MAXN],pre[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].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;
}
void floyd()   //floyd求最短路
{
    maxxx=-1;
    for(int k=1;k<=f;k++){
        for(int i=1;i<=f;i++){
            for(int j=1;j<=f;j++){
                if(mp[i][k]+mp[k][j]<mp[i][j]) mp[i][j]=mp[i][k]+mp[k][j];
            }
        }
    }
    for(int i=1;i<=f;i++){   //找出最短路中的最大值
        for(int j=1;j<=f;j++){
            if(mp[i][j]!=INF && mp[i][j]>maxxx) maxxx=mp[i][j];
        }
    }
}

void Build_Graph(ll mid)  //建图(邻接表)
{
    int i,j;
    init();
    FRE(i,1,f){
        addedge(s,i,date[i][0]);
        addedge(f+i,t,date[i][1]);
    }
    FRE(i,1,f)
    FRE(j,1,f)
    if (mp[i][j]<=mid)
        addedge(i,j+f,inf);
}

bool ok(ll mid) //二分判断
{
    Build_Graph(mid);
    int ans=sap(s,t,t+1);
    if (ans>=sum) return true;
    return false;
}

void solve()  //二分
{
    int i,j;
    ll l=0,r=maxxx,ans=-1;
    while (l<=r)
    {
        ll mid=(l+r)/2;
        if (ok(mid)) {
                r=mid-1;
                ans=mid;
        }
        else l=mid+1;
    }
    printf("%lld\n",ans);
}

int main()
{
    int i,j;
    while(~scanf("%d%d",&f,&p))
    {
        sum=0;
        for(i=1;i<=f;i++){
            for(j=1;j<=f;j++)
                mp[i][j]=INF;
            mp[i][i]=0;
        }
        int u,v;
        ll w;
        s=0,t=2*f+1;
        FRE(i,1,f)
        {
            scanf("%d%d",&date[i][0],&date[i][1]);
            sum+=date[i][0];
        }
        FRE(i,1,p)
        {
            scanf("%d%d%lld",&u,&v,&w);
            if (w<mp[u][v])
                mp[u][v]=mp[v][u]=w;
        }
        floyd();
        solve();
    }
    return 0;
}
/*
3 4
7 2
0 4
2 6
1 2 40
3 2 70
2 3 90
1 3 120
4 3
4 0
2 2
0 2
0 2
1 2 2
2 3 2
2 4 2
*/


Ombrophobic Bovines (poj 2391 网络流+二分+Floyd)

标签:ombrophobic bovines   poj 2391   网络流+二分+floyd   

原文地址:http://blog.csdn.net/u014422052/article/details/43917135

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!