设为首页 收藏本站
查看: 694|回复: 0

[经验分享] poj 2391 Ombrophobic Bovines(floyd + 二分 + Sap)

[复制链接]
发表于 2015-9-21 09:19:51 | 显示全部楼层 |阅读模式
Ombrophobic Bovines
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 12440Accepted: 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[j] = oo;
        }
    }
}

void Add( int u, int v, int w ){
    e[index].v = v;
    e[index].w = w;
    e[index].next = pre;
    pre = 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[k] + map[k][j] < map[j] ){
                    map[j] = map[k] + map[k][j];
                    high = max( high, map[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 = t+1;
    while ( dis < t+1 ){
        for ( i = cur; i != -1; i = e.next ){
            if ( e.w > 0 && dis[e.v] + 1 == dis ){
                break;
            }
        }
        cur = i;
        if ( cur != -1 ){
            flow = min( flow, e.w );
            front[e.v] = u;
            u = e.v;
            if ( u == t ){
                maxflow += flow;
                for ( i = front[t]; ; i = front ){
                    e[cur].w -= flow;
                    e[cur^1].w += flow;
                    if ( i == s ){
                        break;
                    }
                }
                flow = INF;
                u = s;
            }
        }
        else{
            int mindis = t+1;
            for ( i = pre; i != -1; i = e.next ){
                if ( e.w > 0 && mindis > dis[e.v] + 1 ){
                    mindis = dis[e.v] + 1;
                    cur = i;
                }
            }
            gap[dis]--;
            if ( !gap[dis] ){
                break;
            }
            dis = mindis;
            gap[mindis]++;
            if ( u != s ){
                u = front;
            }
        }
    }
    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, &prot);
            total += num;
        }
        while ( m-- ){
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            if ( map[v] > w ){
                map[v] = map[v] = 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 );
                Add( i, i+n, INF );
                Add( i+n, 2*n+1, prot );
                for ( int j = 1; j <= n; j++ ){
                    if ( map[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;
}




来自为知笔记(Wiz)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-116534-1-1.html 上篇帖子: 将EXCEL上载到SAP内表(转自JACK WU) 下篇帖子: SAP SD订价条件技术分析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表