poj 2391 Ombrophobic Bovines(floyd + 二分 + Sap)

Ombrophobic Bovines
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12440 Accepted: 2745

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

 

求最大值最小问题
 
拆点:要限制最大流量,防止出现,1->2 = 1,2->3 = 1,  ==> 1->3 = 2这种情况,这就需要拆点了
 
做法:floyd + 二分 + Sap
 
#include <iostream>
#include <string>
#define maxn 440
#define ll __int64
const ll oo = 2000000000000;
#define INF 2000000000
#define max(a, b) a > b ? a : b
#define min(a, b) a < b ? a : b
 
using namespace std;
 
ll map[maxn][maxn];
int num[maxn];
int prot[maxn];
int pre[maxn];
int front[maxn];
int gap[maxn];
int cur[maxn];
int dis[maxn];
int n;
int index;
 
struct Edges{
    int v;
    int w;
    int next;
}e[82000];
 
void init(){
    for ( int i = 0; i <= n; i++ ){
        for ( int j = 0; j <= n; j++ ){
            map[i][j] = oo;
        }
    }
}
 
void Add( int u, int v, int w ){
    e[index].v = v;
    e[index].w = w;
    e[index].next = pre[u];
    pre[u] = index++;
 
    e[index].v = u;
    e[index].w = 0;
    e[index].next = pre[v];
    pre[v] = index++;
}
 
ll Floyd(){                    
    ll high = 0;
    for ( int k = 1; k <= n; k++ ){
        for ( int i = 1; i <= n; i++ ){
            for ( int j = 1; j <= n; j++ ){
                if ( i == k || k == j || i == j ){
                    continue;
                }
                if ( map[i][k] + map[k][j] < map[i][j] ){
                    map[i][j] = map[i][k] + map[k][j];
                    high = max( high, map[i][j] );
                }
            }
        }
    }
    return high;
}
 
ll maxflow_Sap( int s, int t ){
    int i;
    memset( dis, 0, sizeof(dis) );
    memset( gap, 0, sizeof(gap) );
    memcpy( cur, pre, sizeof(cur) );
    int u = s;
    ll flow = INF, maxflow = 0;
    gap[s] = t+1;
    while ( dis[s] < t+1 ){
        for ( i = cur[u]; i != -1; i = e[i].next ){
            if ( e[i].w > 0 && dis[e[i].v] + 1 == dis[u] ){
                break;
            }
        }
        cur[u] = i;
        if ( cur[u] != -1 ){
            flow = min( flow, e[i].w );
            front[e[i].v] = u;
            u = e[i].v;
            if ( u == t ){
                maxflow += flow;
                for ( i = front[t]; ; i = front[i] ){
                    e[cur[i]].w -= flow;
                    e[cur[i]^1].w += flow;
                    if ( i == s ){
                        break;
                    }
                }
                flow = INF;
                u = s;
            }
        }
        else{
            int mindis = t+1;
            for ( i = pre[u]; i != -1; i = e[i].next ){
                if ( e[i].w > 0 && mindis > dis[e[i].v] + 1 ){
                    mindis = dis[e[i].v] + 1;
                    cur[u] = i;
                }
            }
            gap[dis[u]]--;
            if ( !gap[dis[u]] ){
                break;
            }
            dis[u] = mindis;
            gap[mindis]++;
            if ( u != s ){
                u = front[u];
            }
        }
    }
    return maxflow;
}
 
int main(){
    int m, total, i;
    ll high;
 
    while ( ~scanf("%d%d", &n, &m) ){
        total = high = 0;
        init();
        for ( i = 1; i <= n; i++ ){
            scanf("%d%d", &num[i], &prot[i]);
            total += num[i];
        }
        while ( m-- ){
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            if ( map[u][v] > w ){
                map[u][v] = map[v][u] = w;
                high = max( high, w );
            }
        }
        ll low = 0, ans = -1;
        ll ma = Floyd();
        high = max( high, ma );
        while ( low <= high ){                //二分无限逼近最小时间
            ll mid = ( low + high ) >> 1;
            index = 0;
            memset( pre, -1, sizeof(pre) );
            for ( i = 1; i <= n; i++ ){
                Add( 0, i, num[i] );
                Add( i, i+n, INF );
                Add( i+n, 2*n+1, prot[i] );
                for ( int j = 1; j <= n; j++ ){
                    if ( map[i][j] <= mid ){
                        Add( i, j+n, INF );
                    }
                }
            }
            ll flow;
            flow = maxflow_Sap( 0, 2*n+1 );
            if ( flow == total ){
                ans = mid;
                high = mid - 1;
            }
            else{
                low = mid + 1;
            }
        }
        printf("%I64d\n", ans);
    }
    return 0;
}
 




posted @ 2013-09-02 11:25  /bin  阅读(229)  评论(0编辑  收藏  举报