POJ 2122 Optimal Milking(二分+最大流)

题目大意:有C个机器,K头牛,每个机器只能服务M头牛
现在给出每两点之间的距离,距离为0表示不可达
现在问:所有的牛都被服务到,且行走的距离的最大值最小值是多少

解题思路:最大值最小化,二分枚举
可以先用floyd处理一下每头牛到每个机器的最小距离

建图的话,就源点连接每头牛,容量为1
每个机器连接汇点,容量为m
现在要连接的是牛到机器的边,只要距离满足枚举距离的就可以连边了,容量为1(因为一条边不可能走两次)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXNODE = 250;
const int MAXEDGE = 100010;
typedef int Type;
const Type INF = 0x3f3f3f3f;

struct Edge {
    int u, v, next;
    Type cap, flow;
    Edge() {}
    Edge(int u, int v, Type cap, Type flow, int next): u(u), v(v), cap(cap), flow(flow), next(next) {}
};

struct ISAP {
    int n, m, s, t;
    Edge edges[MAXEDGE];
    int head[MAXNODE], p[MAXNODE], num[MAXNODE], cur[MAXNODE], d[MAXNODE];
    bool vis[MAXNODE];

    void init(int n) {
        this->n = n;
        memset(head, -1, sizeof(head));
        m = 0;
    }

    void AddEdge(int u, int v, Type cap) {
        edges[m] = Edge(u, v, cap, 0, head[u]);
        head[u] = m++;
        edges[m] = Edge(v, u, 0, 0, head[v]);
        head[v] = m++;
    }

    bool BFS() {
        memset(vis, 0, sizeof(vis));
        queue<int> Q;
        for (int i = 0; i < n; i++)
            d[i] = INF;
        d[t] = 0;
        vis[t] = 1;
        Q.push(t);

        while (!Q.empty()) {
            int u = Q.front(); Q.pop();

            for (int i = head[u]; ~i; i = edges[i].next) {
                Edge &e = edges[i ^ 1];
                if (!vis[e.u] && e.cap > e.flow) {
                    vis[e.u] = true;
                    d[e.u] = d[u] + 1;
                    Q.push(e.u);
                }
            }
        }
        return vis[s];
    }

    Type Augment() {
        int u = t;
        Type flow = INF;
        while (u != s) {
            Edge &e = edges[p[u]];
            flow = min(flow, e.cap - e.flow);
            u = edges[p[u]].u;
        }

        u = t;
        while (u != s) {
            edges[p[u]].flow += flow;
            edges[p[u] ^ 1].flow -= flow;
            u = edges[p[u]].u;
        }
        return flow;
    }

    Type Maxflow(int s, int t) {
        this->s = s; this->t = t;
        Type flow = 0;
        BFS();
        //如果s-->t走不通
        if (d[s] >= n)
            return 0;

        memset(num, 0, sizeof(num));
        for (int i = 0; i < n; i++)
            cur[i] = head[i];

        for (int i = 0; i < n; i++)
            if (d[i] < INF) 
                num[d[i]]++;

        int u = s;
        while (d[s] < n) {
            if (u == t) {
                flow += Augment();
                u = s;
            }

            bool ok = false;//纪录是否找到了下一个点
            for (int i = cur[u]; ~i; i = edges[i].next) {
                Edge &e = edges[i];
                if (e.cap > e.flow && d[u] == d[e.v] + 1) {
                    ok = true;
                    p[e.v] = i;//点v由第i条边增广得到
                    cur[u] = i;//尝试到第i条边
                    u = e.v;
                    break;
                }
            }

            //如果没找到下一个点,表示u到t的最短路要变长了,或者没路可走了
            if (!ok) {
                //找寻u到下一个点的最短路
                int Min = n - 1;
                for (int i = head[u]; ~i; i = edges[i].next) {
                    Edge &e = edges[i];
                    if (e.cap > e.flow)
                        Min = min(Min, d[e.v]);
                }
                if (--num[d[u]] == 0)//GAP优化
                    break;
                num[d[u] = Min + 1]++;
                cur[u] = head[u];
                //返回前一个点,因为该点的最短距离已经变了
                if (u != s)
                    u = edges[p[u]].u;
            }
        }
        return flow;
    }
}isap;

#define maxn 250
int k, c, m, source, sink;
int dis[maxn][maxn];

void build(int mid) {

    source = 0, sink = k + c + 1;
    isap.init(sink + 1);
    for (int i = 1; i <= k; i++)
        isap.AddEdge(i, sink, m);
    for (int i = 1; i <= c; i++)
        isap.AddEdge(source, i + k, 1);

    for (int i = k + 1; i <= k + c; i++)
        for (int j = 1; j <= k; j++) 
            if (dis[i][j] <= mid) isap.AddEdge(i, j, 1);
}

void solve() {

    int l = 0, r = 21000, mid;
    while (r >= l) {
        mid = (r + l) / 2;
        build(mid);
        if (isap.Maxflow(source, sink) == c) r = mid - 1;
        else l = mid + 1;
    }
    printf("%d\n", r + 1);
}

void floyd() {
    for (int l = 1; l <= k + c; l++)
        for (int i = 1; i <= k + c; i++)
            for (int j = 1; j <= k + c; j++)
                if (dis[i][j] > dis[i][l] + dis[l][j])
                    dis[i][j] = dis[i][l] + dis[l][j];
}

void init() {
    for (int i = 1; i <= k + c; i++)
        for (int j = 1; j <= k + c; j++) {
            scanf("%d", &dis[i][j]);
            if (!dis[i][j]) dis[i][j] = INF;
        }
    floyd();
}

int main() {
    while (scanf("%d%d%d", &k, &c, &m) != EOF) {
        init();
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值