POJ-2391 Ombrophobic Bovines (二分答案+Floyd+拆点+最大流)

题目链接:https://cn.vjudge.net/contest/176665#problem/G

对网络流进行二分答案,总觉得容易超时,,,没勇气。

题解:

先通过一遍Floyd,得到任意两点间的最短距离,不走冤枉路,汇点T,每一个filed有一个have->已有的牛的数量,can->能放得下的牛,因为时间相互覆盖,所以,直接计算时间比较有困难性,发现有二分性质,短的时间可以,那么更长的时间一定可以,所以进行二分答案。
二分答案为时间,然后验证答案的正确性,建立网络流,S指向个点,容量为have,各点指向T,容量为can,也就表示出了有sum(have)的牛,跑向了各个点,然后又分配到了各个点,因为有时间答案的限制,所以,只能添加两点最短距离小于答案的边到图中,这样,完整的图就建立出来,然后跑最小割,如果最小割等于sum(have),也就是满流,那么这个答案可行,缩小答案范围,继续验证。
注意,需要拆点,否则会发生串流,2->3 and 3->4 != 2->4。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const LL N = 510;
const LL M = 540010;
const LL INF = 0x3f3f3f3f3f3f3f3f;

struct edge {
    LL to, next;
    LL c, f;
}graph[M];
LL totlen;
LL head[N];

LL n, m; 
LL have[N], can[N];
LL e[N][N];
LL s, t;
LL sum;

void init(){
    totlen = 0;
    memset(head, -1, sizeof head);
}

void addEdge(LL u, LL v, LL w) {
    graph[totlen] = {v, head[u], w, 0};
    head[u] = totlen++;
    graph[totlen] = {u, head[v], 0, 0};
    head[v] = totlen++;
}

void Floyd(LL n) {
    for(LL k = 1; k <= n; k++) {
        for(LL i = 1; i <= n; i++) {
            for(LL j = 1; j <= n; j++) {
                e[i][j] = min(e[i][j], e[i][k]+e[k][j]);
            }
        }
    }
}

// Dinic 
LL que[N];
LL cur[N];
LL level[N];
bool dinic_bfs(LL s, LL t) {
    LL front = 0, tail = 0;
    memset(level, 0, sizeof level);
    level[s] = 1;
    que[tail++] = s;
    while(front != tail) {
        LL u = que[front++];
        if(front == N) front = 0;
        for(LL i = head[u]; i != -1; i = graph[i].next) {
            LL v = graph[i].to;
            if(!level[v] && graph[i].c-graph[i].f > 0) {
                level[v] = level[u]+1;
                que[tail++] = v;
                if(tail == N) tail = 0;
            }
        }
    }
    return level[t];
}

// cpflow: can pass flow  到达u点最大能通过的流量
LL dinic_dfs(LL u, LL t, LL cpflow) { 
    if(u == t) return cpflow;  // 到达汇点
    // u 点到其他点 最多能增广的流量, 最多不能超过cpflow,由前面的边限制
    LL addflow = 0;  
    for(LL& i = cur[u]; i != -1; i = graph[i].next) {
        LL v = graph[i].to;
        if(level[v] == level[u]+1 && graph[i].c-graph[i].f > 0) {
            addflow = dinic_dfs(v, t, min(graph[i].c-graph[i].f, cpflow));
            if(addflow > 0) {
                graph[i].f += addflow;  // 正向通过的流量加
                graph[i^1].f -= addflow;  // 反向的流量就得减
                return addflow;        // 增广成功,直接返回。 
            }
        }
    }
    return addflow;
}

LL dinic(LL s, LL t) {
    LL maxflow = 0;
    LL addflow = 0;
    while(dinic_bfs(s, t)) {
        memcpy(cur, head, sizeof head);  // 提高效率 
        while((addflow = dinic_dfs(s, t, INF)) > 0)
            maxflow += addflow;
    }
    return maxflow;
}

bool check(LL mid) {
    init();
    for(LL i = 1; i <= n; i++) {
        addEdge(s, i, have[i]);
        addEdge(i+n, t, can[i]);
        addEdge(i, i+n, sum);
    }
    for(LL i = 1; i <= n; i++) {
        for(LL j = i+1; j <= n; j++) {
            if(e[i][j] <= mid) {
                addEdge(i, j+n, sum);
                addEdge(j, i+n, sum);
            }
        }
    }
    LL ans = dinic(s, t);
    return ans >= sum;
}

int main() {
    // cout << 0x3f3f3f3f << endl;
    // cout << 0x3f3f3f3f3f3f3f3f << endl;
    while(scanf("%lld%lld", &n, &m) != EOF) {
        sum = 0;
        s = 0, t = n*2+1;
        for(int i = 0; i <= n; i++) {
            for(int j = 0; j <= n;j ++) {
                e[i][j] = INF;
            }
        }
        // cout << e[0][0] << " " << e[0][1] << endl;

        for(LL i = 1; i <= n; i++) {
            scanf("%lld%lld", &have[i], &can[i]);
            sum += have[i];
        }
        for(LL i = 1; i <= m; i++) {
            LL a, b, c;
            scanf("%lld%lld%lld", &a, &b, &c);
            if(c < e[a][b])
                e[a][b] = e[b][a] = c;
        }

        Floyd(n);
        LL l = 1, r = INF-1;
        LL ans = -1;
        while(l <= r) {
            LL mid = (l+r)/2;
            if(check(mid)) {
                r = mid-1;
                ans = mid;
            }else{
                l = mid+1;
            }
            // cout << l << " " << r << " " << mid << endl;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
发布了19 篇原创文章 · 获赞 10 · 访问量 7518
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览